Solved

Behavior if DateTIme is null

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

861 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