How are you handling DBNull exceptions in Typed Datasets?

How are you handling DBNull exceptions in Typed Datasets?

Post by <tbo.. » Fri, 10 May 2002 00:25:05

I want to load controls on a form from a typed dataset that may or may not
have DBNull in some of the fields in the resultset.  Whenever I try to
reference a field that has a DBNull value, the typed dataset throws an
exception.  I looked at the generated dataset class, and I see property get
is trying to convert a DBNull to String which causes the exception.  How
have you handled this?  Any suggestions?

VB.Net, Stored Procedures, SQL 2000




1. Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

When I use a dataset generated from a .xsd file, and bind it to a DataGrid,
everything works fine.  Datagrid goes through the fields, and displays them,
and if it finds a DBNull it just displays a blank cell, even in cases where
you are talking about Integer, date, and other fields.  I know how to tell
the generated dataset to return String.Empty when it encounters a DBNull,
but that doesn't apply to strongly typed fields such as integer and
DateTime, etc.

I have a custom collection inheriting from the CollectionBase class, I can
bind it to the (web) DataGrid and it works fine until it gets a null value,
at which point it still calls my property accessors, which should return an
Int value, generating exception on a null value.  However, if I bind the
generated dataset to the Datagrid it works fine.  It doesn't appear that
when I bind the dataset directly that it even CALLS the datarow's accessors,
or calls the IsXYZNull() function to see if it is null.

Does anybody know at all what the DataRow/DataTable objects do that allow
the DataGrid to bind to it, and not call those accessors, thus allowing it
to always return String.Empty?  In doing research (and looking at
source code) it appears that since it inherits the IListSource Interface,
DataGrid calls GetList() which returns a DataView of itself.  Its not very
easy to track what exactly is happening though, since the VS.NET debugger
can't step through any of the datarow/datatable code, so I can watch what
its doing).

Originally what I hoped would happen is that the DataGrid would call a
generic accessor, perhaps the Item, with a string value for the column, and
inside there if I saw DBNull I could return String.Empty, and the DataGrid
could just display that.

I've read through as many messages as I could find through google groups
that are remotely related to this issue, and I have anticipated some of the
questions/responses I will get

1a) Use annotations to make the underlying dataset return String.Empty
1b) This is not feasible with non-string fields

2a) Using the annotations, return a default integer value, such as zero
2b) In my database there is a huge difference between 0 and Null, putting 0
in the display would tell the user something that is highly incorrect

3a) Return an exception
3b) This already occurs, and is what I wish to avoid

4a) Return null
4b) This would cause an exception when it tried to cast a null as an Int32

If there is anybody that can shed light on this area of DataBinding to the
System.Web.UI.WebControls.DataGrid object, I would be eternally grateful, as
I have been fighting this issue for 2 weeks.


Mark E. Scott Jr.

2. comments on emacs running on win95 or winnt

3. How do you handle DBNull and strongly typed datasets?

4. What is CG with Java?

5. DBNull Value exception in a Typed DataSet...

6. IIxi SIMMs

7. Exception raised when dbnull value is returned to a typed dataset.

8. PRI-ISDN backup

9. Typed Dataset and empty string to DBNULL on Update

10. Strongly Typed DataSet, Date Field and DBNull

11. Typed datasets and DBnull

12. TextBox, DbNull and strong typed dataset

13. System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid