Solved

Check for datareader null value

Posted on 2004-09-16
6
3,781 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 83 total points
ID: 12077503

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

Assisted Solution

by:vigrid
vigrid earned 83 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 84 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now