Hi all,
As many of you know, I'm new to .NET and I'm getting ready to do my first "real" project. I've run in to an issue that seems pretty common from reading the posts on the internet. I was just wanting to hear how our local experts address the subject.
I'm creating a little order entry database. This is a WinForms app accessing a SQLServer database. I've created a strongly-typed dataset to do my data access. I have a customer class that uses the dataset to pull the data in and then expose the various information as properties. I'm then using the binding source bound to my customer object. So far so good and man really slick stuff too! Everything is fine until I hit a NULL value in the data. When I try to assign the data value to my standard .NET type property, I get the "cannot cast" error because of the null.
I've read several solutions online - the two most prominant being:
- creating a conversion function that returns a generic value. i.e. if object is null then return DateTime.MinValue else return (DateTime)object.
- using the nullable datatype (int?, DateTime?, etc...)
The first approach doesn't seem that great because I want to bind my date fields to a DatePicker control. Assigning some arbitrary date sounds problematic - i.e. I don't really want my datepicker to say "1/1/1900" if they haven't entered a date. Does that mean I'd end up writing code every time to avoid that? The second option sounded great and worked perfect until I ran the app - it appears if the bound date is null then the datepicker defaults to today, which really puts me in the same situation.
Can those of you who have dealt with this issue share some insight on a good overall strategy?
Thanks in advance -- Jason