Solved

Behavior if DateTIme is null

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

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 32

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 74

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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now