Solved

Behavior if DateTIme is null

Posted on 2015-01-27
5
113 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 34

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 34

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

Industry Leaders: 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

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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