0

I use EF model-first in my application and use stored procedures too.

My table has a LastStatus column name of datetime datatype and it's nullable.

When the user fills in a datetime value, it works correctly.

Here is my code :

  (@StartAcceptDate IS NULL OR LastStatusDate >= @StartAcceptDate)
  AND  (@EndAcceptDate IS NULL OR LastStatusDate >= @EndAcceptDate)

But when user want to select all record release the date.

Many records have null value.

My question is here: how to pass null as a datetime from C# to SQL Server?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

The best way is to set the defaults parameters value to NULL in your stored procedure and you don't have to explicitly pass NULL to your stored procedure.

CREATE PROCEDURE dbo.Proc 
    (@param1 INT = NULL, 
     @param2 INT = NULL ....)

If you still want to send NULL from your C# code to your stored procedure, you can use DBNull.Value

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mojtaba Tajik
  • 1,725
  • 16
  • 34
0

You can declare your DateTime as nullable like this:

DateTime? StartAcceptDate;
DateTime? EndAcceptDate;

That way you can asign the value of null to the variables and send them that way to the database.

Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32