Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTime value null 1

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
G'morning,

I have a SQL field "TermDate" of type "date" and the value of which can be null.

I have an object called Assoc that has a TermDate property of type DateTime. When I atempt to load the object with the values from the DB, it barfs when it encounters the DB's null value in the date field.

I thought about setting the TermDate Property to type string, but that involves all sorts of converting to DateTime and then back to a string again all over the place.

Can anyone give some advice about how to handle this?

Thanks!

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
assuming .net 2.0 or higher.
Code:
class Assoc
{
   public DateTime? TermDate {get;set;}
}
which is syntax sugar for
Code:
class Assoc
{
   public Nullable<DateTime> TermDate {get;set;}
}
when you load the object
Code:
var assoc = new Assoc();
if(row["TermDate"] == DbNull.Value)
{
   assoc.TermDate = null;
}
else
{
   assoc.TermDate = (DateTime)row["TermDate"];
}
you will also find that .net and ms sql DateTime min/max values are different and will cause problems as well. You cannot save DateTime.Min or DateTime.Max to the sql database. you will need to convert the min/max values to/from the database.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
jmeckley,

Thanks for the quick reply and for sharing your knowledge.

It works like a charm !

Chew


10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top