Solved

check for null datetime

Posted on 2009-05-15
5
1,810 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 32

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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