Solved

extremely easy

Posted on 2012-03-30
9
268 Views
Last Modified: 2012-03-30
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?
0
Comment
Question by:1jaws
9 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 37788529
check it for being == DBNull.Value before using it.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37788539
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
 
LVL 22

Expert Comment

by:p_davis
ID: 37788540
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:n2fc
ID: 37788542
Use an if statement ahead of that one that tests for null first!
If null, take appropriate action and skip the conversions...
0
 

Author Closing Comment

by:1jaws
ID: 37788555
thanks needed exactly this
0
 
LVL 22

Expert Comment

by:p_davis
ID: 37788578
some days -- i get headaches from trying to help around here. </whine>
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37788582
Why You No Use Int.TryParse? [Weekend fun;)]
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37788608
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
 
LVL 22

Expert Comment

by:p_davis
ID: 37788617
s'all good.... but thanks anyway kaufmed
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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