Solved

Check for datareader null value

Posted on 2004-09-16
6
3,786 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

9 Experts available now in Live!

Get 1:1 Help Now