NULL Value - Specified cast is not valid

F

fniles

I am accessing MS Access db with tblA whose column UNABLE can have NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidCastException - Specified cast is
not valid." either when I do
IsDBNull(dr.GetString(0))
or
dr.GetString(0)
How can I fix this problem ?

Thanks.

Dim sUnable as string
Dim sql As String
Dim dr As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand

sql = "select UNABLE from tblA where ACCOUNT = 'xyz'"
With cmd
.Connection = g_ConnectionDemoOLE
.CommandText = sql
dr = .ExecuteReader()
End With
Do While dr.Read
If bErr Then
sUnable = ""
Else
If IsDBNull(dr.GetString(0)) Then sUnable = "" Else sUnable =
dr.GetString(0) --> ERROR HERE
End If
If bErr Then sUnable = ""
Loop
 
J

Jim Wooley

I am accessing MS Access db with tblA whose column UNABLE can have
NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidCastException - Specified
cast is
not valid." either when I do
IsDBNull(dr.GetString(0))
or
dr.GetString(0)
How can I fix this problem ?
Thanks.

You need to check the IsDBNull on the column, not the result of the column.
Thus you should change it to:

If dr.IsDBNull(0) then MyVal = dr.GetSTring(0)

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

That is because you are calling the GetString method before checking if
the value is null. Use the IsDBNull method:

If dr.IsDBNull(0) Then sUnable = String.Empty Else sUnable = dr.GetString(0)
 
J

Jay B. Harlow [MVP - Outlook]

fniles,
As the other suggest I normally use dr.IsDBNull(0) also.

However the "problem" you are having is that dr.GetString is attempting to
return a String, when you have a Null. You then attempt to pass this String
to the IsDBNull function. You need to pass an Object to the IsDBNull
function, you can use need to use dr.GetValue to retrieve this object then
use the IsDBNull function:

If IsDBNull(dr.GetValue(0)) Then

However as I stated, I simply use dr.IsDBNull instead...

| If dr.IsDBNull(0) Then sUnable = "" Else sUnable =
| dr.GetString(0)

As it avoids "getting" the value twice...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I am accessing MS Access db with tblA whose column UNABLE can have NULL
| value. When I access UNABLE whose value is null, I got an error "
| Run-time exception thrown : System.InvalidCastException - Specified cast
is
| not valid." either when I do
| IsDBNull(dr.GetString(0))
| or
| dr.GetString(0)
| How can I fix this problem ?
|
| Thanks.
|
| Dim sUnable as string
| Dim sql As String
| Dim dr As OleDb.OleDbDataReader
| Dim cmd As New OleDb.OleDbCommand
|
| sql = "select UNABLE from tblA where ACCOUNT = 'xyz'"
| With cmd
| .Connection = g_ConnectionDemoOLE
| .CommandText = sql
| dr = .ExecuteReader()
| End With
| Do While dr.Read
| If bErr Then
| sUnable = ""
| Else
| If IsDBNull(dr.GetString(0)) Then sUnable = "" Else sUnable =
| dr.GetString(0) --> ERROR HERE
| End If
| If bErr Then sUnable = ""
| Loop
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top