?
Solved

check for null datetime

Posted on 2009-05-15
5
Medium Priority
?
1,812 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 2000 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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

741 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