More Specific Error Messages???

  • Thread starter Thread starter David Lozzi
  • Start date Start date
D

David Lozzi

Howdy,

I'm doing some simple database reading and loading severl values into labels for readonly use. Here is my script:

Dim selStoreID As String

If StoreID.SelectedIndex > -1 Then
selStoreID = StoreID.SelectedItem.Value
'lblStoreID.Text = StoreID.SelectedItem.Text

Dim sqlConn As String = Session("SQLConn")
Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

Dim queryString As String = "SELECT * FROM tblStores WHERE ID = " & selStoreID
Dim dbCommand As IDbCommand = New SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open()
Dim rec As IDataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
If Not rec.Read() Then
'error info
Else
btnSelect.Visible = True
Dim strStoreID As String = rec("StoreNo")
lblStoreID.Text = rec("StoreNo")
If Not rec("address1") Is System.DBNull.Value Then
lblAddress1.Text = rec("Address1")
End If
If Not rec("address2") Is System.DBNull.Value Then
lblAddress2.Text = rec("Address2")
End If
If Not rec("city") Is System.DBNull.Value Then
lblCity.Text = rec("City")
End If
If Not rec("state") Is System.DBNull.Value Then
lblState.Text = rec("state")
End If
If Not rec("zip") Is System.DBNull.Value Then
lblZip.Text = rec("zip")
End If
If Not rec("phone") Is System.DBNull.Value Then
If Len(rec("phone")) = 10 Then
Dim fon As String = rec("phone")
lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon, 4, 3) & "-" & Right(fon, 4)
End If
End If
End If
Else
lblStoreID.Text = "None"
btnSelect.Visible = False
End If

Now, the error I get is "Cast from type 'DBNull' to type 'String' is not valid", but it doesn't tell me what line! How can I figure that out? I was getting this error before until I added the check for the DBNull.Value for each field, but there is on record where I still receive this error. Here's the data in the record. Other records work fine!!

ID StoreNo Address1 Address2 City State Zip Phone CompanyID
43294 7068 4732 DEVINE ST COLUMBIA SC 0 4

So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am catching that too??

Thanks!!!!
 
Put a breakpoint and see what the debugger says. Or put a print statement in the code outputting the IDs for each item, then when the error occurs, you know it is the most recent to be outputted.
Howdy,

I'm doing some simple database reading and loading severl values into labels for readonly use. Here is my script:

Dim selStoreID As String

If StoreID.SelectedIndex > -1 Then
selStoreID = StoreID.SelectedItem.Value
'lblStoreID.Text = StoreID.SelectedItem.Text

Dim sqlConn As String = Session("SQLConn")
Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

Dim queryString As String = "SELECT * FROM tblStores WHERE ID = " & selStoreID
Dim dbCommand As IDbCommand = New SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open()
Dim rec As IDataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
If Not rec.Read() Then
'error info
Else
btnSelect.Visible = True
Dim strStoreID As String = rec("StoreNo")
lblStoreID.Text = rec("StoreNo")
If Not rec("address1") Is System.DBNull.Value Then
lblAddress1.Text = rec("Address1")
End If
If Not rec("address2") Is System.DBNull.Value Then
lblAddress2.Text = rec("Address2")
End If
If Not rec("city") Is System.DBNull.Value Then
lblCity.Text = rec("City")
End If
If Not rec("state") Is System.DBNull.Value Then
lblState.Text = rec("state")
End If
If Not rec("zip") Is System.DBNull.Value Then
lblZip.Text = rec("zip")
End If
If Not rec("phone") Is System.DBNull.Value Then
If Len(rec("phone")) = 10 Then
Dim fon As String = rec("phone")
lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon, 4, 3) & "-" & Right(fon, 4)
End If
End If
End If
Else
lblStoreID.Text = "None"
btnSelect.Visible = False
End If

Now, the error I get is "Cast from type 'DBNull' to type 'String' is not valid", but it doesn't tell me what line! How can I figure that out? I was getting this error before until I added the check for the DBNull.Value for each field, but there is on record where I still receive this error. Here's the data in the record. Other records work fine!!

ID StoreNo Address1 Address2 City State Zip Phone CompanyID
43294 7068 4732 DEVINE ST COLUMBIA SC 0 4

So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am catching that too??

Thanks!!!!
 
hi,

my suggestion is the error in all checking lines with the DBNull.. so try to
correct like this.

wrong: If Not rec("zip") Is System.DBNull.Value Then
correct: If Not rec("zip") Is System.DBNull Then


the problem is when you take value.. it is a string type. but rec("zip")
returns a DBNull.

hope this helps,
CMA


Howdy,

I'm doing some simple database reading and loading severl values into labels
for readonly use. Here is my script:

Dim selStoreID As String

If StoreID.SelectedIndex > -1 Then
selStoreID = StoreID.SelectedItem.Value
'lblStoreID.Text = StoreID.SelectedItem.Text

Dim sqlConn As String = Session("SQLConn")
Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

Dim queryString As String = "SELECT * FROM tblStores WHERE ID =
" & selStoreID
Dim dbCommand As IDbCommand = New SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open()
Dim rec As IDataReader =
dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
If Not rec.Read() Then
'error info
Else
btnSelect.Visible = True
Dim strStoreID As String = rec("StoreNo")
lblStoreID.Text = rec("StoreNo")
If Not rec("address1") Is System.DBNull.Value Then
lblAddress1.Text = rec("Address1")
End If
If Not rec("address2") Is System.DBNull.Value Then
lblAddress2.Text = rec("Address2")
End If
If Not rec("city") Is System.DBNull.Value Then
lblCity.Text = rec("City")
End If
If Not rec("state") Is System.DBNull.Value Then
lblState.Text = rec("state")
End If
If Not rec("zip") Is System.DBNull.Value Then
lblZip.Text = rec("zip")
End If
If Not rec("phone") Is System.DBNull.Value Then
If Len(rec("phone")) = 10 Then
Dim fon As String = rec("phone")
lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon,
4, 3) & "-" & Right(fon, 4)
End If
End If
End If
Else
lblStoreID.Text = "None"
btnSelect.Visible = False
End If

Now, the error I get is "Cast from type 'DBNull' to type 'String' is not
valid", but it doesn't tell me what line! How can I figure that out? I was
getting this error before until I added the check for the DBNull.Value for
each field, but there is on record where I still receive this error. Here's
the data in the record. Other records work fine!!

ID StoreNo Address1 Address2 City
State Zip Phone CompanyID
43294 7068 4732 DEVINE ST COLUMBIA SC
0 4

So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am
catching that too??

Thanks!!!!
 
No such luck. VS.NET throws a blue line under it stating 'DBNull' is a type
in 'System' and cannot be used as an expression.

Here's another thing that is weird, is that this script is working fine this
morning. I didn't change a thing from last night! Lets see if it remains!

Thanks
 
Back
Top