Thursday, July 7, 2011

I choose nothing...or maybe it should be empty.

I've encountered some times when exceptions are thrown because of a null value in the database. For my current project, it doesn't matter if a column contains a null value. In fact, there are many null values because much of the data that can be entered is optional. Whenever I encountered one of these peskly null exceptions, I tried to handle it in code, but then I stumbled upon something much easier. Much easier.

The colunns of datatables are set to throw an exception for null values when the column is read - exactly under what conditions I'm not quite sure but I have run into them. But you don't have to live with that. You can choose to return a value "Empty"  or "Nothing". I chose "Nothing" and my (coding) life was suddenly made easier. Except...when I tried to read values from the datatable and apply them as a datasource for a combobox, my combobox wasn't being populated. It was very strange because I was populating four comboboxes and one of them was working but the comboboxes for the other three were blank. But when I chose "Empty" instead of "Nothing", my comboboxes worked. So I guess there is a difference. 

I stumbled on this by looking at the properties of the datatable column. Go to the Edit DataSet with Designer and then go to a column in one of your tables and select it. In the Properties window you can see there is a selection for NullValue.