By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

NULL Value - Specified cast is not valid

P: n/a
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


May 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
May 4 '06 #2

P: n/a
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)

fniles wrote:
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

May 4 '06 #3

P: n/a
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
"fniles" <fn****@pfmail.com> wrote in message
news:%2****************@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
|
|
|
|
May 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.