473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

More Specific Error Messages???

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
Nov 19 '05 #1
5 1092
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:es**************@TK2MSFTNGP15.phx.gbl...
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
Nov 19 '05 #2
CMA
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:es**************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #3
Did that work for you David!!
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #4
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" <cm**************@textcentric.lk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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:es**************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #5
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!
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lkrubner | last post by:
This is a general computer question, but I'm writing in PHP so I'll post this to comp.lang.php. I've been writing a content management system. I've a Singleton object that keeps track of all...
5
by: zorhel | last post by:
Hi. My clients will be IE, Mozilla and Opera in a Windows and *nix OS. So, my web app need to, from a server, send messages to a specific client (browser), send messages for all clients,...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
6
by: Brian Gideon | last post by:
How have you handled the finalization of thread-specific unmanaged resources? My question pertains specifically to using the DDEML which is a thread-specific API. In other words, every call to...
2
by: Odd Bjørn Andersen | last post by:
When I run a backup script from the Task Center i get these messages: 2006-02-15-21.30.08.515000+060 E21985H390 LEVEL: Error PID : 2408 TID : 3416 PROC :...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
2
by: Ted | last post by:
1) In several tables, in my MySQL version, I created columns using something like the following: `ab_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, This...
0
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm developing a web page displaying a list of messages, the code (GetInboxMessages) below is the method which gives the a list of messages to this web page's reapeat control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.