Solved

Behavior if DateTIme is null

Posted on 2015-01-27
5
107 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:knowlton
  • 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:knowlton
ID: 40575687
Thank you both!

Tom
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

770 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