By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,333 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,333 IT Pros & Developers. It's quick & easy.

Null DB fields causing an error

P: n/a
Below is the code I am using to display a list of values from a table row the
problem is some of these columns may be null. Currently when I execute this
code I get the following error: "Cast from type 'DBNull' to type 'String' is
not valid."

This error is caused by the null values. How can I avoid this error?

Dim i As Integer = QueueReader.GetOrdinal("CustomerId")

If QueueReader.Read() And Not QueueReader.IsDBNull(i) Then
lblJanuary.Text = QueueReader("January")
lblFebruary.Text = QueueReader("February")
lblMarch.Text = QueueReader("March")
lblApril.Text = QueueReader("April")
lblMay.Text = QueueReader("May")
lblJune.Text = QueueReader("June")
lblJuly.Text = QueueReader("July")
lblAugust.Text = QueueReader("August")
lblSeptember.Text = QueueReader("September")
lblOctober.Text = QueueReader("October")
lblNovember.Text = QueueReader("November")
lblDecember.Text = QueueReader("December")
End If
Thanks, Justin
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
isn't it possible that one of the months has a null value in it?

if so you need to check for null on each of them.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Below is the code I am using to display a list of values from a table row the problem is some of these columns may be null. Currently when I execute this code I get the following error: "Cast from type 'DBNull' to type 'String' is not valid."

This error is caused by the null values. How can I avoid this error?

Dim i As Integer = QueueReader.GetOrdinal("CustomerId")

If QueueReader.Read() And Not QueueReader.IsDBNull(i) Then
lblJanuary.Text = QueueReader("January")
lblFebruary.Text = QueueReader("February")
lblMarch.Text = QueueReader("March")
lblApril.Text = QueueReader("April")
lblMay.Text = QueueReader("May")
lblJune.Text = QueueReader("June")
lblJuly.Text = QueueReader("July")
lblAugust.Text = QueueReader("August")
lblSeptember.Text = QueueReader("September")
lblOctober.Text = QueueReader("October")
lblNovember.Text = QueueReader("November")
lblDecember.Text = QueueReader("December")
End If
Thanks, Justin

Nov 19 '05 #2

P: n/a
For the fields that could contain NULL values, you must do a check for
DBNULL

Below is an example of one way.

For me I write a function for the page, so my code would look like the
following

Dim i As Integer = QueueReader.GetOrdinal("CustomerId")

If QueueReader.Read() And Not QueueReader.IsDBNull(i) Then
If Not IsDBNull(QueueReader("January")) Then lblJanuary.Text
= QueueReader("January")
If Not IsDBNull(QueueReader("February")) Then
lblFebruary.Text = QueueReader("February")
'And the rest would take similar form
End If
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Below is the code I am using to display a list of values from a table row
the
problem is some of these columns may be null. Currently when I execute
this
code I get the following error: "Cast from type 'DBNull' to type 'String'
is
not valid."

This error is caused by the null values. How can I avoid this error?

Dim i As Integer = QueueReader.GetOrdinal("CustomerId")

If QueueReader.Read() And Not QueueReader.IsDBNull(i) Then
lblJanuary.Text = QueueReader("January")
lblFebruary.Text = QueueReader("February")
lblMarch.Text = QueueReader("March")
lblApril.Text = QueueReader("April")
lblMay.Text = QueueReader("May")
lblJune.Text = QueueReader("June")
lblJuly.Text = QueueReader("July")
lblAugust.Text = QueueReader("August")
lblSeptember.Text = QueueReader("September")
lblOctober.Text = QueueReader("October")
lblNovember.Text = QueueReader("November")
lblDecember.Text = QueueReader("December")
End If
Thanks, Justin

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.