Rad & Dot Net - Digital Warrior

Assorted ramblings (and vents!!) from a techie on the sunny shores of Kenya, East Africa

Monday, January 03, 2005

Null Dates & Typed Datasets

Making heavy use of the typed dataset generally works 99.99% of the time, but things get unglued when you run into a situation where you define one that has DateTime property that you want to be nullable.

Setting minOccurs to 0 is fine, but the instant you define the null value as null, it refuses to compile. If you leave it as blank it will compile, but if you fill the dataset and the DateTime field happens to be null, a runtime error is thrown!

Hmmm.

So as a workaround, you have to put something in the field to make sure everything runs. My workaround is to use a certain date to represent a null value, so I happily did this:

ProductData.ProductRow row = New.ProductData.ProductRow();

//not using SelectedValue becuase you might run into a 1.0 runtime!

row.ProductID = int.Parse(cbProductID.SelectedItem.Value);

row.Name = Utils.ParseText(txtName.Text);

row.Description = Utils.ParseText(txtDescription.Text);

if
(txtStartdate.Text == "")

row.Startdate
= DateTime.Parse(Utils.ParseDate(txtStartdate.Text));

else

row.Startdate = DateTime.MinValue

row.EndDate
= DateTime.Parse( Utils.ParseText(txtEndDate.Text))



Problem with this is that SQL Server and .NET do not agree on what the smallest date should be, and so it throws a runtime error, since the .NET minimum date is too small to be stored in SQL server. As far as .NET is concerned, the smallest date value is just about 0AD (Which makes storage of Roman Census data a bit of a problem! Is this backward compatibility with the abacus or what?!!). As far as SQL server is concerned the smallest date value possible is about 1st january 1753.

So to work around this little snafu instead of DateTime.MinValue create a new date as above.

Now the only thing left is to modify the UI code to behave itself when presented with this particular date, since it strictly means there is no data. A quick and dirty workaround is to handle the OnDataBinding method, and reset the date to the current date.

By the way, is it just me or is the DatePicker's method of handling a lack of data too awkward? That funny little checkbox, as far as I'm concerned is not intuitive at all! If it is checked does it mean that it is null or that it has data? And the reason that our OnDataBinding override is set to the current date is to spare the more arthritic among us from clicking themselves into the grave shifting the date from when we begun celebrating New Year's Day to the current date

0 Comments:

Post a Comment

<< Home