Unable to cast object of type 'System.DBNull' to type 'System.String' in table adapter

  • Thread starter Thread starter Fred Dag
  • Start date Start date
F

Fred Dag

Hi,
I have a table adapter created using the DataSet gui in VS2005. The code it
generates returns this error:

System.InvalidCastException: Unable to cast object of type 'System.DBNull'
to type 'System.String'.

Line 3224: get {
Line 3225: try {
Line 3226: return
((string)(this[this.tableITEM.DescriptionColumn]));
Line 3227: }
Line 3228: catch (System.InvalidCastException e) {The my code
looks like this:items =
itemAdapter.GetREFItemDataByItemNumber(rEFItemNumber);string description;if
(Convert.IsDBNull(items[0].Description)){description = "";}else{description
= items[0].Description;}As far as I can tell the error is in the code
generated by the table adapter and not my code. How do I solve this
problem?Thanks in advance.
 
Strongly Typed DataSet? This is quite common.

If so there should be an IsColumnNameNull() method to check for null before
attempting to retrieve:

if(!ds.MyTable[0].IsColumnIDNull())
{
valuePulled = ds.MyTable[0].ColumnID;
}

If not strongly typed, you will have to poke at the value and compare to
DBNull prior to geetting.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
Back
Top