Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check for datareader null value

Posted on 2004-09-16
6
Medium Priority
?
3,798 Views
Last Modified: 2012-06-27
I have a datareader that fills certain fields:

txtFirstName.Text = dataReader.GetString(8);
txtLastName.Text = dataReader.GetString(9);
txtCompany.Text = dataReader.GetString(10);

Every user has a first and last name, but not all of them have a company.  It blows up with a null value, so how do I check for null and skip that line?

txtFirstName.Text = dataReader.GetString(8);
txtLastName.Text = dataReader.GetString(9);
if(datareader.SOMETHING != null)txtCompany.Text = dataReader.GetString(10);


thanks
dave
0
Comment
Question by:KaiserSose
6 Comments
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 332 total points
ID: 12077503

if(datareader[10] != DBNull.Value)
txtCompany.Text = dataReader.GetString(10);
0
 
LVL 4

Assisted Solution

by:vigrid
vigrid earned 332 total points
ID: 12077610
or use the

if(!System.Convert.IsDbNull(dataReader[10]))
  txtCompany.Text = dataReader.GetString(10);
0
 
LVL 1

Accepted Solution

by:
micaiah earned 336 total points
ID: 12080872
if(!datareader.IsDBNull(10)) {
   // do some stuff
} else {
   // don't do some stuff b/c it will blow up
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:KaiserSose
ID: 12083574
I was doing it like this:  

 if(dataReader.IsDBNull(10)){}else txtCompany.Text = dataReader.GetString(10);

but then you kickstarted my brain and reminded me that i can use the ! so now its like this:

if(!dataReader.IsDBNull(10)) txtCompany.Text = dataReader.GetString(10);

All three of you had good answers so I'm splitting the points.

dave
0
 
LVL 9

Expert Comment

by:msdixon
ID: 12083733
sometimes that's all we need... a good kick upside the head! my wife makes sure i get that whenever i need it...   :-)
0
 

Expert Comment

by:jerra
ID: 33103375
String stringvalue = odbcDataReader.IsDBNull(0) ? null : odbcDataReader.GetString(0);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question