tags:

views:

55

answers:

2

What .NET method has this error message: "Object cannot be cast from DBNull to other types"

The stack traces have been stipped from my logs so I'm looking for a starting point.

Answers:

  • Convert.ToInt32: "Object cannot be cast from DBNull to other types."
  • VB's CInt: "Conversion from type 'DBNull' to type 'Integer' is not valid."
  • C#'s (int): "Specified cast is not valid."
  • Int32.Parse(x.ToString()): "Input string was not in a correct format."
+2  A: 

It's probably a Convert.To_ method or something similar, but I don't know that that will give you much insight into finding it. This error is raised when you're trying to use a value you got from a database (and it's null), though, so I would start by looking at where you're getting your data from.

tloflin
Yes, Reflector shows, that this messages comes out of the `System.Convert` class (at least that is one of the places'). The message text is stored in the resources of mscorlib.dll
0xA3
Yep, that was the one. Thanks.
Jonathan Allen
A: 

It sounds to me like you're attempting to pull data from a DataReader/DataTable/ResultSet and bind it to a text field or web control item.

Typically you need to test for IsDBNull before doing those kind of binding, or you'll get the error message specified.

Dillie-O
I know I'm supposed to check for nulls. What I was asking is which conversion/casting method had that particular message so that I could find the errant code and fix it.
Jonathan Allen