Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 987
codingbiz Avatar asked Jun 07 '12 13:06

codingbiz


1 Answers

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))
like image 155
Romil Kumar Jain Avatar answered Sep 25 '22 08:09

Romil Kumar Jain