How can someone handle default datetime
Asked Answered
T

6

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.

Turbinate answered 7/6, 2012 at 13:6 Comment(1)
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.Lindbom
M
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))
Merrile answered 7/6, 2012 at 13:8 Comment(4)
+1. When checking for null, either that or value.HasValue, whatever floats your boat :)Rood
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.Napkin
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 DBTurbinate
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.Merrile
W
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)
Warmblooded answered 7/6, 2012 at 13:12 Comment(1)
painful when you have to do this in a GridView. Then you will have to use TemplateField instead of BoundFieldTurbinate
E
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;
Eared answered 7/6, 2012 at 13:8 Comment(0)
S
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>.

Swage answered 7/6, 2012 at 13:11 Comment(2)
DateTime.MinValue = 1/1/01. It's the same thing.Turbinate
yes, but it's a defined field. it's trivial for the grid to check for equality between DateTime.MinValue and whatever the DAL is giving backSwage
E
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.)

Egocentrism answered 7/6, 2012 at 13:13 Comment(5)
Note: new DateTime() is the same as DateTime.MinValue.Holoenzyme
@HansKesting Interesting, since it appears to be an invalid date for some systems. (From MSDN: "The value of this constant is equivalent to 00:00:00.0000000, January 1, 0001." --> must be out of range for some systems.)Egocentrism
SqlServer will not accept this date (will reject dates before 1753 IIRC)Holoenzyme
"If I Recall Correctly" - I'm not 100% sure about that 1753, but it's something like thatHoloenzyme
So you can't use SqlServer's built in types in databases for historic information :-)Egocentrism
F
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.

Frazier answered 7/6, 2012 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.