check for null datetime

I have an input form that handles a null date of birth

datetime dt
customer.Dob = DateTime.TryParse(Dob.Text, out dt) ? dt : (DateTime?)null;

Now I want to do the same when populating the form

I load in customer class and populate webform ...

customerDOBtextbox.Text = ????????

How can I check for null date when loading the form?  If its null then just don't display anything in the dob textobx
Who is Participating?
pivarConnect With a Mentor Commented:

Is this what you look for?

customerDOBtextbox.Text = (customer.Dob == null ? "" : customer.Dob.ToString());


Jamie McAllister MVPSharePoint ConsultantCommented:
if (customerDOBtextbox.Text.Length > 0)



if (!String.IsNullOrEmpty(dt.ToString())) {
    customerDOBtextbox.Text = dt.ToShortDateString();

Shaun KlineLead Software EngineerCommented:
If you are loading the date from the nullable date time, you need to check the HasValue property. You can use: if (customer.Dob.HasValue) customerDOBtextbox.Text = customer.Dob.Value;

If you are loading the value from a database object, such as a typed data set and your data field is nullable, you can check the Is<fieldname>Null property of the data object.

If you are reading the value from a data reader, you need to check the reader.IsDBNull(reader.GetOrdinal("<fieldname>")) before trying to access the value.
mugseyAuthor Commented:
Thanks for your postings guys
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.