Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handling null's when extracting from db

Status
Not open for further replies.

Tokhra

Programmer
Oct 1, 2003
134
ES
Hi all,

I have the following code:

this.Address = (string) dataReader["Address"];

which creates an invalid cast exception if it returns null, whats the best way to handle this? I don't really want to have if (dataReader["Address"] != DbNull.Value) for every db extraction..there must be a better way?

Thanks,
Matt.
 
for some reason it took away the [ and ] (brackets) around the address when I submitted the post.
 
Yep, that's how you do it -- you have to check every field that is potentially nullable against DBNull.

You can make this easier by putting that code into it's own function (I would suggest one function for every datatype -- one for Ints, one for String, one for DateTime, etc).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks chiph - I've done what you suggested, works a treat :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top