Solved

Behavior if DateTIme is null

Posted on 2015-01-27
5
110 Views
Last Modified: 2015-01-28
UNLabelValueLiabilityDate.Text = OrganizationData.dayLiabilityInsuranceDate.ToShortDateString();

is returning:

bad date
This is due to a null value being returned?  

How do I account for null values coming back?
0
Comment
Question by:Tom Knowlton
[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
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40573820
Actually that is the DateTime.MinValue, but something like would work for you:
If (OrganizationData.dayLiabilityInsuranceDate != DateTime.MinValue)
	UNLabelValueLiabilityDate.Text = OrganizationData.dayLiabilityInsuranceDate.ToShortDateString();
else
	UNLabelValueLiabilityDate.Text = "Invalid Date";

Open in new window

Or if you want to use a ternary operation:
UNLabelValueLiabilityDate.Text = OrganizationData.dayLiabilityInsuranceDate != DateTime.MinValue ? DateTime.MinValueOrganizationData.dayLiabilityInsuranceDate.ToShortDateString() : "Invalid Date";

Open in new window

-saige-
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 200 total points
ID: 40573841
If you are wondering how I know it is the DateTime.MinValue.

First the DateTime.MinValue is well-documented.

Second, because if it were a null value you would get a null-reference exception.  Consider the following:
using System;

namespace EE_Q28605038
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime min = DateTime.MinValue;
			DateTime? nad = null;
			Console.WriteLine(min.ToShortDateString());
			Console.WriteLine(nad.Value.ToShortDateString());
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output:Capture.JPGBut we could use the above method (by adding a null check) to still print the proper results:
using System;

namespace EE_Q28605038
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime min = DateTime.MinValue;
			DateTime? nad = null;
			Console.WriteLine(min.ToShortDateString());
			Console.WriteLine(nad != null && nad.Value != DateTime.MinValue ? nad.Value.ToShortDateString() : "Invalid Date");
			Console.ReadLine();
		}
	}
}

Open in new window

Now produces the following results:Capture.JPG-saige-
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40574368
Second, because if it were a null value you would get a null-reference exception...
That assumes the type in play is Nullable<DateTime>, which it is not...it's a DateTime. Those are two different, albeit somewhat related types. DateTimes can never be null whereas Nullable<DateTime> can.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 40574371
How do I account for null values coming back?
Coming back from where? A database? If so, then you need to be checking the column for DBNull.Value, not null.
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 40575687
Thank you both!

Tom
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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