Failure to handle no value from Access Table

August 15, 2011 at 02:48:47
Specs: Windows 7
Hello chaps,

I am writing a VB.Net program to retreive the database field values from a table in which several columns may contain "no values" in some rows.


Database format: "Access"
Error message: A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

my progam failed while running these few lines.

---------Code-------
Dim s_BurialAft As String
Dim s_BurialType As String
Dim s_Depth As String

.....
Do While Not pTableRow is nothing
.....
'error occurs when it runs to the below line

s_BurialAft = pTableRow.Value(i_xBurialAft) 'i_x_BurialAft is the column index no s_BurialType = pTableRow.Value(i_xBurialType) '_xBurialType is the column index no
s_Depth = pTableRow.Value(i_xDepth) 'i_xDepth is the column index no
.....
pTableRow = pTableRow.NextRow
Loop

--- end of code ---


But when i converted the few lines of codes:
s_BurialAft = CType(pTableRow.Value(i_xBurialAft), String)
s_BurialType = CType(pTableRow.Value(i_xBurialType), String)
s_Depth = CType(pTableRow.Value(i_xDepth), String)
Error message: (Same message)
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Can anybody find out what problem here. Or how to test null / no values out before assigning to the string variables.

Thanks a lot!

E Lau


See More: Failure to handle no value from Access Table

Report •


#1
August 15, 2011 at 21:15:58
Hello

I found out the following in the Access Table. Some tables refers the "no value" fields as <Null>. But some tables refers the "no value" field as "" (blank)

Error message: A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

So, I test whether the field values of the columns are null or not now.

If Convert.IsDBNull(pTableRow.Value(i_xBurialAft)) Then
s_BurialAft = ""
Else
s_BurialAft = pTableRow.Value(i_xBurialAft)
End If
If Convert.IsDBNull(pTableRow.Value(i_xBurialType)) Then
s_BurialType = ""
Else
s_BurialType = pTableRow.Value(i_xBurialType)
End If
If Convert.IsDBNull(pTableRow.Value(i_xDepth)) Then
s_Depth = ""
Else
s_Depth = pTableRow.Value(i_xDepth)
End If

It is fine and successful.

Thanks
E Lau


Report •
Related Solutions


Ask Question