Solved

check for null datetime

Posted on 2009-05-15
5
1,805 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:mugsey
5 Comments
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
Comment Utility
if (customerDOBtextbox.Text.Length > 0)
{}
0
 
LVL 18

Expert Comment

by:carlnorrbom
Comment Utility

Hi,

Try:

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

/Carl.
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
Comment Utility
Hi,

Is this what you look for?

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

/peter

0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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.
0
 

Author Comment

by:mugsey
Comment Utility
Thanks for your postings guys
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now