I would declare an empty String variable like this:
string myString = string.Empty;
Is there an equivalent for a 'DateTime' variable ?
Update :
The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL. E.g:
DateTime? someDate = null;
myCommand.Parameters.AddWithValue("@SurgeryDate", someDate);
When I run this code an exception is catched telling me the 'StoredProcedure' expected a '@SurgeryDate' parameter. But i provided it. Any idea why?
If you set the date to
The value is set to {1/1/0001 12:00:00 AM}
The method you used (
AddWithValue
) doesn't convertnull
values to database nulls. You should useDBNull.Value
instead:This will pass the
someDate
value if it is notnull
, orDBNull.Value
otherwise. In this case correct value will be passed to the database.This will work for null able dateTime parameter
. .
Since
DateTime
is a value type you cannot assignnull
to it, but exactly for these cases (absence of a value)Nullable<T>
was introduced - use a nullableDateTime
instead:Option 1: Use a nullable DateTime?
Option 2: Use DateTime.MinValue
Personally, I'd prefer option 1.
The .addwithvalue needs dbnull. You could do something like this:
or use a method extension...