At first, I thought you needed to change your stored procedure.
Now that I've read the question again, I've realized that the error message comes from the c# side, not from the stored procedure (that I still think you should change).
Attempting to convert a null or empty string to DateTime
will result with the error in your question. To avoid that, you need to make sure the string can in fact be converted to DateTime
before sending it to the stored procedure:
DateTime datetime;
DateTime? olusturulmaTarihi = null;
if(DateTime.TryParse(dataList.olusturulmaTarihi, out datetime))
{
olusturulmaTarihi = (DateTime?)datetime;
}
var query = ctx.onayListele(olusturulmaTarihi).ToList();
This way, you will send null
to the stored procedure if the string can't be parsed as DateTime
, and avoid the error.
As to the stored procedure, I would suggest writing it like this instead:
ALTER PROCEDURE [dbo].[onayListele]
@in_olusturmaTarihi date = NULL
AS
BEGIN
SELECT Onay.onayID,
alep.olusturulmaTarihi,
TalepTuru.talepTuruAdi,
TalepDurumu.talepDurumuAciklamasi
FROM Onay
WHERE @var_olusturmaTarihi IS NULL
OR CONVERT(date,Talep.olusturulmaTarihi) = @var_olusturmaTarihi
END
Please note that if you have an index on Talep.olusturulmaTarihi
, this stored procedure will not be able to use it. In that case, you better use something like this instead:
ALTER PROCEDURE [dbo].[onayListele]
@in_olusturmaTarihi date = NULL
AS
BEGIN
SELECT Onay.onayID,
alep.olusturulmaTarihi,
TalepTuru.talepTuruAdi,
TalepDurumu.talepDurumuAciklamasi
FROM Onay
WHERE @var_olusturmaTarihi IS NULL
OR
(
Talep.olusturulmaTarihi >= CAST(@var_olusturmaTarihi as datetime) -- or whatever the data type of the column is
AND Talep.olusturulmaTarihi < DATEADD(DAY, 1, CAST(@var_olusturmaTarihi as datetime)) -- or whatever the data type of the column is
)
END