Solved

Check for datareader null value

Posted on 2004-09-16
6
3,789 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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