Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > More Specific Error Messages???

Reply
Thread Tools

More Specific Error Messages???

 
 
David Lozzi
Guest
Posts: n/a
 
      12-16-2004
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.CloseConne ction)
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!!!!

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      12-16-2004
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.
"David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message news:...
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.CloseConne ction)
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!!!!

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


 
Reply With Quote
 
 
 
 
CMA
Guest
Posts: n/a
 
      12-16-2004
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


"David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message
news:...
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.CloseConne ction)
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!!!!

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com



 
Reply With Quote
 
Patrick Olurotimi Ige
Guest
Posts: n/a
 
      12-16-2004
Did that work for you David!!
Patrick



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
David Lozzi
Guest
Posts: n/a
 
      12-16-2004
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

--
David Lozzi
Web Applications/Network Specialist
Delphi Technology Solutions, Inc.
dlozzi(remove-this)@delphi-ts.com


"CMA" <> wrote in message
news:%...
> 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
>
>
> "David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message
> news:...
> 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.CloseConne ction)
> 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!!!!
>
> --
> David Lozzi
> Web Applications/Network Specialist
> Delphi Technology Solutions, Inc.
> dlozzi(remove-this)@delphi-ts.com
>
>
>



 
Reply With Quote
 
Patrick Olurotimi Ige
Guest
Posts: n/a
 
      12-16-2004
Well David some magic do happen sometimes OVERNIGHT
Eazy..
Patrick



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML parsing problem finding a specific element in a specific place mazdotnet ASP .Net 2 10-02-2009 10:07 AM
Parsing DOM to search specific tags with specific custom attribute William FERRERES Javascript 7 07-09-2007 08:11 PM
Is ViwState Page-Specific or UserControl-Specific =?Utf-8?B?SmF2?= ASP .Net 2 08-16-2006 09:30 PM
redirect traffic on specific ip to specific interface mimiseh Cisco 3 06-05-2005 09:14 PM
How do you make sure a frameset is loaded? I'm trying to open a frameset in a new window which shows a specific html page in a specific frame ck388 Javascript 1 09-24-2003 08:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57