1
I’m conducting a search through StoredProcedures
in the EntityFramework
and the field of Data
this presenting error conversion to type String
.
Method
public IEnumerable GetFilteredList(ConsultaBanner filtro)
{
string sql = "spAdmin_Banner_ConsultaBannerPeloFiltro @titulo={0},"
sql += "@apresentaCookie={1}";
sql += ",@vigenciaInicial={2},@vigenciaFinal={3},@tipoSistema={4}";
return context.Banners.SqlQuery(sql
, filtro.titulo// != null ? filtro.titulo : null
,filtro.apresentaCookie //!= null ? filtro.apresentaCookie : null
,filtro.vigenciaInicial.ToString() //!= null ? filtro.vigenciaInicial : null
, filtro.vigenciaFinal.ToString() //!= null ? filtro.vigenciaFinal : null
,filtro.tipoSistema //!= null ? filtro.tipoSistema : null
).ToList();
}
This above code worked without the error of convert
, but did not return anything so I did some tests on SQL Server
and I realized that the Tadas fields were being passed as ''
, so I changed the StoredProcedure
to convert ''
for null
and the error conversion happened again.
Storedprocedure
alter PROCEDURE [dbo].[spAdmin_Banner_ConsultaBannerPeloFiltro]
@titulo as varchar(100) = null
,@apresentaCookie as bit = null
,@vigenciaInicial as datetime = null
,@vigenciaFinal as datetime = null
,@tipoSistema as tinyint = null
as
if(@vigenciaInicial='')
begin
set @vigenciaInicial=null;
end
if(@vigenciaFinal='')
begin
set @vigenciaFinal=null;
end
select
id ,titulo,mensagem,apresentaCookie
,convert(varchar(10), vigenciaInicial, 103) [vigenciaInicial]
,convert(varchar(10), vigenciaFinal, 103) [vigenciaFinal]
,tipoSistema [tipoSistemaId]
,case when tipoSistema = 1 then 'Prestador'
when tipoSistema = 2 then 'Cliente' when tipoSistema = 3
then 'Login' end [tipoSistema]
from banner
where 0 = 0
and ( (titulo like '%' + @titulo + '%' and @titulo is not null)
or @titulo is null)
and ( (apresentaCookie = @apresentaCookie and @apresentaCookie is not null)
or @apresentaCookie is null)
and ( ((vigenciaInicial >= @vigenciaInicial and vigenciaFinal <= @vigenciaFinal)
and (@vigenciaInicial is not null and @vigenciaFinal is not null ))
or (@vigenciaInicial is null or @vigenciaFinal is null) )
and ( (tipoSistema = @tipoSistema and @tipoSistema is not null)
or @tipoSistema is null)
Someone’s been through this trouble?
The error:
The 'vigenciaInicial' Property on 'Banner' could not be set to a 'System.String' value. You must set this Property to a non-null value of type 'System.Datetime'.
UPDATE
Both calls below return value after the treatment of ''
exec spAdmin_Banner_ConsultaBannerPeloFiltro @titulo = null,@apresentaCookie = null,@vigenciaInicial = null,@vigenciaFinal = null,@tipoSistema = null
or
exec spAdmin_Banner_ConsultaBannerPeloFiltro @titulo = null,@apresentaCookie = null,@vigenciaInicial = '',@vigenciaFinal = '',@tipoSistema = null
Script Table
CREATE TABLE [dbo].[Usuario](
[Usuarioid] [int] IDENTITY(1,1) NOT NULL,
[CpfCnpJ] [varchar](20) NOT NULL,
[Nome] [varchar](120) NOT NULL,
[sexo] [char](1) NULL,
[DataNascimento] [datetime] NULL,
[senhaAcesso] [varchar](200) NOT NULL,
[lembreteSenha] [varchar](200) NULL,
[ativo] [bit] NULL,
[DataCadastro] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[Usuarioid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Your trial still returns nothing, the error is that now instead of
vazio
('') she’s returningnull
and Ienumerable cannot be null.– George Wurthmann
@Georgewurthmann you speak of the values or the same result? When I take the treatise of '' from the trial it works without presenting any feedback. When I add the treatment the error occurs.
– Krismorte
I speak of the return of Procedure, from what I understand now it is returning NULL, as its method expects the return of a Ienumerable it from error. If this is really what is happening change to when there is no data, return empty and not null.
– George Wurthmann
@Georgewurthmann added 2 Procedure call examples and both return value via SQL.
– Krismorte
You still have the problem?
– novic
@Virgilionovic could not use Procedure so I converted the query to Linq
– Krismorte
It went wrong because the parameters have to be like
– novic
@Virgilionovic tried to use the Sqlparameter and it didn’t work either.
– Krismorte
I’ll make a minimal example
– novic
If you can provide the script for this table?
– novic
@Virgilionovic don’t have here now, tomorrow I post the table script.
– Krismorte
And then you solved your problem?
– novic