checking for dbnull values - sqldatareader ordinals

Hi There,

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

Sean


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



LVL 8
MrAgileAsked:
Who is Participating?
 
sognoctConnect With a Mentor Commented:
try :
class.propertyname =  reader.IsDBNull(reader.GetOrdinal("prova")) ? 0 : reader.GetInt32(reader.GetOrdinal("myID"));
0
 
Joel CoehoornDirector of Information TechnologyCommented:
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.
0
 
sognoctConnect With a Mentor Commented:
class.propertyname =  reader.IsDBNull(reader.GetOrdinal("myID")) ? 0 : reader.GetInt32(reader.GetOrdinal("myID"));
0
 
MrAgileAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.