Home All Groups Group Topic Archive Search About

NULL Value - Specified cast is not valid

Author
3 May 2006 6:56 PM
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

Author
7 May 2006 10:43 PM
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


Show quoteHide quote
"fniles" <fni***@pfmail.com> wrote in message
news:%237eWNy6bGHA.3380@TK2MSFTNGP04.phx.gbl...
|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
|
|
|
|
Author
7 May 2006 10:43 PM
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


Show quoteHide quote
"fniles" <fni***@pfmail.com> wrote in message
news:%237eWNy6bGHA.3380@TK2MSFTNGP04.phx.gbl...
|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
|
|
|
|