Solved

check for null datetime

Posted on 2009-05-15
5
1,809 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
ID: 24394284
if (customerDOBtextbox.Text.Length > 0)
{}
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24394635

Hi,

Try:

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

/Carl.
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 24394670
Hi,

Is this what you look for?

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

/peter

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24394734
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
ID: 24395199
Thanks for your postings guys
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 76
vb.net convert long time to mm:ss 23 22
c# DateTime Format validation 4 68
Visual C# Windows Project - Clone Form 4 22
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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