tags:

views:

130

answers:

3
+1  Q: 

DateTime Variable

I want to pass a null value for a DateTime variable in C#. The value should be stored in the database as null.

I've tried using Datetime.Minvalue, but that stores a default value. It has to be a null in database. How do I do that?

+8  A: 

Use DateTime? as in

DateTime? foo = null;

That makes a nullable DateTime:

A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

then when writing the value out, you can use the value like this:

if(foo == null)
{
   // Handle the null case
} 
else 
{
   // Handle the non-null case
}
Daniel LeCheminant
+1  A: 

You could use DateTime? values type. It can have null values but otherwise it's completely the same as DateTime

vava
ah you are few seconds too late.
Sung Meister
@Sung: Fastest gun in the west syndrome :-/
Daniel LeCheminant
@Daniel: When I refreshed the page, timestamp said, 2 seconds ago, but I already saw 2 answers
Sung Meister
Mine was second, I saw a yellow line about new answer the moment I've clicked a button
vava
+5  A: 

I agree with the nullable type answer, but when you go to write it to the database, you still need to test for null and convert it to DBNull.Value.

jhale