checking for dbnull values - sqldatareader ordinals

Posted on 2007-12-04
Medium Priority
Last Modified: 2013-12-16
Hi There,

What's the syntax for testing for sqldatareader ordinals for null?I tried something like below but it's not working.


           class.propertyname = reader.GetOrdinal("myID")== DBNull.Value ? 0 : reader.GetInt32(reader.GetOrdinal("myID"));

Question by:MrAgile
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
  • 2

Accepted Solution

sognoct earned 2000 total points
ID: 20401857
try :
class.propertyname =  reader.IsDBNull(reader.GetOrdinal("prova")) ? 0 : reader.GetInt32(reader.GetOrdinal("myID"));
LVL 18

Expert Comment

by:Joel Coehoorn
ID: 20403896
Sounds like what you really want to do is have a COALESCE statement in the sql used to create the reader, to that a NULL value is never returned in the first place.

Assisted Solution

sognoct earned 2000 total points
ID: 20403957
class.propertyname =  reader.IsDBNull(reader.GetOrdinal("myID")) ? 0 : reader.GetInt32(reader.GetOrdinal("myID"));

Author Comment

ID: 20406655
hi jcoehoorn,

I have done this already. I am just making sure that there is no way this thing is going to fall over in a nasty way.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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