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?
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With