473,378 Members | 1,467 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,378 software developers and data experts.

Null DB fields causing an error

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
2 1148
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pbuck | last post by:
I am running a report that is drawing in data from 5 different queries. The report has five different fields and a sum field to tally up the values in the 5 different fields. The data is loaded...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
7
by: Dan | last post by:
I'm fairly new to C# and I am having some problems understanding int variables and null values. I have created a class that populates the values of it's fields from a DataReader in it's...
9
by: Fish Womper | last post by:
I am at best a part time developer of Access databases. I use Access 2.0, as this is all my employer has on its computers. Even so, to use this ancient version requires a fairly convoluted...
3
by: David W. Fenton | last post by:
I ran into a weird problem with a subreport with an OnFormat event of the subreport detail. When I view the field list in report design view, all the aliased fields in the recordsource are...
2
by: Bob Stearns | last post by:
I have successfully designed a trigger to make sure a set of fields are unique if not null. However it fails in one case: when the duplication occurs among a set of rows being updated...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
2
by: NewNetUser | last post by:
I am new to VB.Net and trying few things to see how everything works. Created a windows form with few text boxes and used ODP to connect to Oracle database programmatically and displayed the fields...
10
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.