preeti
preeti

Reputation: 17

input null in datetime (C# and WPF)

I have a Datepicker named dob and a variable dt that is of type DateTime. to convert my datepicker input to DateTime I'm using ToDateTime conversion as follows

DateTime dt;
dt = Convert.ToDateTime(dob.Text);

and then store dt in a database with its data type in MySQL as datetime.

My problem is that if I do not pick a dateTime my program crashes. My error is "String was not recognized as a valid DateTime"

I tried writing

if (String.IsNullOrEmpty(dob.Text))
{

       dt = null;
}

but dt cannot be null. Is there a way I can have dt as null and be stored as null in the table? Thank you ^^

Upvotes: 1

Views: 183

Answers (1)

Bonny4
Bonny4

Reputation: 301

Try using DateTime?:

DateTime? dt;
// Other code
dt = null; // Will be possible

Upvotes: 1

Related Questions