How can someone handle default datetime

codingbiz picture codingbiz · Jun 7, 2012 · Viewed 12.1k times · Source

I have DAL where I convert database null value to their equivalent representation in C#. For example:

NULL for Numeric = 0
NULL for String = String.Empty
NULL for DateTime = "1/1/0001" (i.e. DateTime.MinValue)

The problem, for date, lies in the presentation layer, especially in GridViews. You cannot show 1/1/01 to users.

What I used to do is check if myDate.Year=1 or myDate.Year < AcceptedDate and display empty string, but seems to be extra effort unlike other types

Please am open to better approach. Thanks.

Answer

Romil Kumar Jain picture Romil Kumar Jain · Jun 7, 2012

Use Nullable datatype to store null value.

DateTime? value = null;
int? myNullableInt = 1;
value = DateTime.Now;

How to check whether variable has value or null

if (value!=null)

String value can store null, so there is no diffrent datatype for string to store null.

string var;
if (var == null)

or

if (String.IsNullOrEmpty(var))