Home All Groups Group Topic Archive Search About

InvalidCastException in DataRow

Author
2 Jun 2006 9:15 PM
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

Author
2 Jun 2006 9:27 PM
Nate
Try

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

or

myTextBox.Text=myDataRow("MyColumn").ToString();

Show quoteHide quote
"Jay Balapa" wrote:

> 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
>
>
>
>
>
>
>
Author
2 Jun 2006 9:41 PM
Jay Balapa
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







Show quoteHide quote
"Nate" <N***@discussions.microsoft.com> wrote in message
news:B6292B49-E924-403F-A592-3C998826A893@microsoft.com...
> Try
>
> if (!myDataRow.IsNull("MyColumn"))
>     myTextBox.Text=myDataRow("MyColumn");
>
> or
>
> myTextBox.Text=myDataRow("MyColumn").ToString();
>
> "Jay Balapa" wrote:
>
>> 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
>>
>>
>>
>>
>>
>>
>>
Author
3 Jun 2006 2:26 AM
Charlie Brown
Try it like this...

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

Its what i use.