Datareaders crashing on null values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

SqlDataReader reader = command.ExecuteReader()
reader.Read()
Name.Value =(string)reader.GetString(1)

if this value is null it crashes, is there a way to determine if the value is null , or can the exception be handled or ignored??
 
You can use the IsDbNull method like DBGod mentions which is what I'd
recommend. You can catch anything though, but in this instance, it's
Definitely not going to be efficient to throw a bunch of exceptions and just
eat them. You can also use IsNull on the SQL Side to supply a default value
if it's null, may or may not be advisable depending on your situation.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
Lane said:
SqlDataReader reader = command.ExecuteReader();
reader.Read();
Name.Value =(string)reader.GetString(1);

if this value is null it crashes, is there a way to determine if the value
is null , or can the exception be handled or ignored??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top