1

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Declare

DateTime ? closure = dateDatumIspisa.SelectedDate;

no need here to use the ?? in this line !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.