Solved

Check for datareader null value

Posted on 2004-09-16
6
3,788 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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