Search code examples
c#asp.netdatabasedata-formats

How can someone handle default datetime


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.


Solution

  • 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))