6

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.

sandes
  • 1,917
  • 17
  • 28
codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • Can't you just adjust the format of the Date Column so that instead of showing 1/1/01 (which is assumed to be 2001) it will show the actual value which is 1/1/0001. That or possible convert the date to a string when it is entered so that it doesn't try and change the date. – Tony318 Jun 07 '12 at 13:11

6 Answers6

10

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))
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • 3
    +1. When checking for null, either that or `value.HasValue`, whatever floats your boat :) – Patryk Ćwiek Jun 07 '12 at 13:12
  • 1
    In addition, you could repeat this pattern throughout your treatment of value types, i.e. NULL for SQLInt could be `int? default = null;`. As a side note, strings are actually reference types, and can be assigned the value `null`. – Jon Senchyna Jun 07 '12 at 13:15
  • I agree. Nullable types are better for this. Why I did not consider this approach is because those fields are required in my DB. But making them nullable in my Business Logic means I have to check value before passing the to DB – codingbiz Jun 07 '12 at 14:13
  • if you don't check for null then null will be updated in the database. What you want to do if these fields has null value. – Romil Kumar Jain Jun 07 '12 at 14:16
6

You can also use DateTime.MinValue constant.

http://msdn.microsoft.com/en-us/library/system.datetime.minvalue.aspx

Your conditions would be:

if (myDate == DateTime.MinValue)
Ihor Deyneka
  • 1,326
  • 1
  • 19
  • 37
  • painful when you have to do this in a GridView. Then you will have to use TemplateField instead of BoundField – codingbiz Jun 07 '12 at 13:16
1

You can use Nullable DateTime, so you will return DateTime? instead of DateTime from your DAL. This way you can check if returned value is null.

DateTime? dateTime = null;
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
1

As the others mention, you could use a System::Nullable<DateTime>.

The other approach I've seen is to use a standard DateTime and just use a special value such as DateTime.MinValue. This is useful if you need to honor an existing interface's types and can't change the DateTime to a Nullable<DateTime>.

jglouie
  • 12,523
  • 6
  • 48
  • 65
1

You can either use a Nullable DateTime as the others suggested, or use this trick: (To prevent non valid defaults.)

// If dateTime has not been initialize, initialize to Now
// (or to any other legal inital values)
dateTime = ((dateTime != new DateTime()) ? dateTime : DateTime.Now);

This trick is useful if you have to use a non-nullable DateTime and want to provide a default if none. (E.g. you have a non-nullable DateTime column in a DB and want to set the value only if row is new.)

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
1

I don't think you have much choice but to make the check like you have been and display accordingly. A nullable type might make things easier for you. Depending on your data, even the numeric should be treated this way. DBNull != 0.

Nigel Whatling
  • 2,371
  • 1
  • 16
  • 22