I'm performing a search through StoredProcedures
in EntityFramework
and the Data
field is displaying conversion error 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 code above worked without the error of convert
, but did not return anything so I did some tests on SQL Server
and realized that fields were being passed as ''
, so I changed StoredProcedure
to convert ''
to null
and the conversion error occurred 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)
Has anyone ever had this problem ?
Error :
The 'InitialValue' property on 'Banner' could not be set to a 'System.String' value. You should set this property to a non-null value of type 'System.DateTime'.
UPDATE
Both calls below return value after 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
Table Script
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]