How to check if DateTime variable is not assigned?

  • Thread starter Thread starter Visual Systems AB \(Martin Arvidsson\)
  • Start date Start date
V

Visual Systems AB \(Martin Arvidsson\)

Hi!

When reading a value from an SQL database i get to a DateTime variable...

How can i check that the there is no value assigned?

Since i cant use if(myDate.ShortDate != null) ?

Regards

Martin
 
Visual said:
Hi!

When reading a value from an SQL database i get to a DateTime
variable...

How can i check that the there is no value assigned?

Since i cant use if(myDate.ShortDate != null) ?

Regards

Martin

If you read a database field that has a null value, then in .Net it has
the value System.DbNull.Value. So it would be something like

if (myDataSet.Tables[0].Rows[rownum]["colname"] != DbNull.Value) ...

This works for *all* types, even strings!

Hans Kesting
 
Hi,

In a DB it will have a special value , DBNull.Value , now when you assign
it to a DateTime you will need to make an assuptiom, like that
DateTime.MinValue or DateTime.MaxValue represent a date with not given
value.

Cheers,
 
Hi,

As DateTime is value type in .net so you just can't simply compare it
with null. Here is one solution. Everytime you declare a DateTime
variable .net automatically assign its the value 1/1/1900 12:00:00 AM
so you can compare it with that.

HTH,
Regards,
Angrez
 
Hi,

I forgot to include a piece of code:

if ( datareader["DateColumn"] == DBNull.Value )
datetimevar = DateTime.MinValue;
else
datetimevar = Convert.ToDateTime( datareader["DateColumn"] );

cheers,
 
Back
Top