Dealing With Null Date Values

Sometimes when I debugging an application, I’ll see  an error issue like this:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

C# doesn’t like null values when it comes to dates.  Thus, I need to deal with that situation in my data models.  If I know that it’s possible there will be a null value in a date variable, I’ll use something like this.

//check null

if (myDate < new DateTime(1753, 1, 2))
myDate = DateTime.Now.AddYears(30);

So as you can see, I addressed the problem by stuffing something in that date value.

Leave a Reply

Your email address will not be published. Required fields are marked *