Thursday, September 13, 2007

Data bound Winforms and NULL

All I want is a null date ....

It seems like a rather reasonable request. I have a date column in my database that allows null, and I have a text box bound to that column through a binding source. When my form loads and the date on the current record is null, the control displays nothing. Great, exactly what I expect.

The problem comes when the the date on the current record is not null, and I want to delete it, or write null back to the database. You can't just blank out the text box, it won't validate. You are forced to enter a validate date, or that magic string that will in fact store your null.

Where does the magic string come from? It's on the Advanced tab of the data binding properties for the control. So why can't no value for the Null Value field mean null? After all, no value displays when reading null from the db!

It's these little puzzling things about data bound forms and controls that keeps me going back to my own data handling. Spending time looking for hacks and workarounds usually outweighs the time savings of a fully bound form. So I find myself with a bit of a hybrid approach, but it's messy.

Alternatives? Not really, .NET is the only game in town for windows desktop apps.