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!!!!
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!!!!