Working with NULL fields in a Strongly Typed DataSet
September 17, 2008 Leave a comment
This entry is here primarily because I have seen this question come up quite a few times. Perhaps when people Google, they will find this entry and avoid asking the question again.
With a strongly typed DataSet, a method is created on each field that can possibly come out of the database as a null. It is Is[Field}Null(). For example, if the field is named Title, the method is IsTitleNull(). So, if you would normally use the following code:
string title = ds.Employee.Title;
or similar, try this:
//You can set this up however you need to
//This is simply an example of one that has to display something
string title = String.Empty;
title = ds.Employee[i].Title;
The other option is to edit the DataSet(s), but if you ever regen, you are hosed again. In other words, editing the DataSet is is kludge. Don’t do it. And, yes, I have seen it done … lots of times.
Peace and Grace,