Reputation: 307
In a database, there is a field that saves a closure date. This date can be NOT NULL only if the case has been closed. If the case is not closed, it has to be NULL. How can I pass null value to a DateTime object?
Tried this but it doesn't work.
DateTime closure= dateDatumIspisa.SelectedDate ?? null;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse("");
DateTime closure= dateDatumIspisa.SelectedDate ?? DBNull.Value;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse(DBNull.Value.ToString());
Also tried GetValueOrDefault() but it inserts DateTime.Min value, while I need this field left empty.
Any suggestions?
Upvotes: 1
Views: 3897
Reputation: 66641
Declare
DateTime ? closure = dateDatumIspisa.SelectedDate;
no need here to use the ?? in this line !
Upvotes: 1
Reputation: 1499860
Just make closure
a DateTime?
instead of a DateTime
. The whole point of Nullable<T>
is to make a nullable type from a non-nullable one.
Now, you haven't shown the type of SelectedDate
- but if it's already a DateTime?
then you don't need to use ??
at all. Just:
DateTime? closure= dateDatumIspisa.SelectedDate;
How familiar are you with nullable value types in general? You might want to read up on the MSDN coverage of them.
Upvotes: 3