InvalidCastException in DataRow

  • Thread starter Thread starter Jay Balapa
  • Start date Start date
J

Jay Balapa

Hello,

This is a compactframework windows app.

Iam trying read from a DataRow.

myTextBox.Text=myDataRow("MyColumn")

If there is a value for the myDataRow("MyColumn") then the statement
executes. But if the column is empty I get invalidCastException, When
casting from number etc..

I have tried CTYPE() or even check for "Is Nothing", just accesing the
column gives me the exception.

How can I prevent the exception from occuring. Underlying DataField is
nvarchar(250) from SQLCE databse.

Thanks
jay
 
Try

if (!myDataRow.IsNull("MyColumn"))
myTextBox.Text=myDataRow("MyColumn");

or

myTextBox.Text=myDataRow("MyColumn").ToString();
 
Thanks Nate for replying.

It is not working
Iam getting the exception on the IF statement you suggested.

Here are the details-
INVALID CAST EXCEPTION WAS UNHANDLED
- COULD NOT FIND RESOURCE ASSEMBLY

-jay
 
Try it like this...

If Not myDataRow("MyColumn") = DbNull.Value
myTextBox.Text=myDataRow("MyColumn")
End If

Its what i use.
 
Back
Top