extremely easy

if (int.Parse(ds.Tables[0].Rows[0]["stateID"].ToString()) != 0)

my stateID is int field on database table and if it is null that blow ups, how can I modify above?
1jawsAsked:
Who is Participating?
 
käµfm³d 👽Commented:
Try:

if (ds.Tables[0].Rows[0]["stateID"] != DBNull.Value && int.Parse(ds.Tables[0].Rows[0]["stateID"].ToString()) != 0)
{
...

Open in new window

0
 
p_davisCommented:
check it for being == DBNull.Value before using it.
0
 
p_davisCommented:
if(ds.Tables[0].Rows.Count > 0 && ds.Tables[0]Rows[0]["stateID"] != DBNull.Value)
{
if (int.Parse(ds.Tables[0].Rows[0]["stateID"].ToString()) != 0)
{}
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
n2fcCommented:
Use an if statement ahead of that one that tests for null first!
If null, take appropriate action and skip the conversions...
0
 
1jawsAuthor Commented:
thanks needed exactly this
0
 
p_davisCommented:
some days -- i get headaches from trying to help around here. </whine>
0
 
Chinmay PatelEnterprise ArchitectCommented:
Why You No Use Int.TryParse? [Weekend fun;)]
0
 
käµfm³d 👽Commented:
p_davis said this just before I did, so I think he should be in on the solution as well. I just posted the example before he did  = )
0
 
p_davisCommented:
s'all good.... but thanks anyway kaufmed
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.