C# handle null value

Hi, experts

Could you please help me debug the following code


for (int i = 0; i < num_R_fuzzy_reuslt; i++)
     {
       DataRow row = DT_fuzzy_reuslt.Rows[i];

      int a = Int32.Parse(row["s_id"].ToString());   // --> if have null value will cause run time error
 }

How can I resolve this issue. thanks in advance
rmtogetherAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lucky_jamesCommented:
put a check:

for (int i = 0; i < num_R_fuzzy_reuslt; i++)
     {
       DataRow row = DT_fuzzy_reuslt.Rows[i];


      int a = (row["s_id"] ! = null)?Int32.Parse(row["s_id"].ToString()): -1;   // --> if have null value will cause run time error
 }
0
lucky_jamesCommented:
above I have used ternary operator. Also I presumed that you want the value of int a to be -1 when the row at that particular s_id is null.

Hope it explains my previous post better. :)
0
rmtogetherAuthor Commented:
is it use if statement?
could you transfer to regular if statement. I have little confuse about it.
thank you
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Om PrakashCommented:
Try
int a = 0
if (row["s_id"] != DBNull.Value)
{
a = Int32.Parse(row["s_id"].ToString());  
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rmtogetherAuthor Commented:
so int can't not assign null to it?
0
SriVaddadiCommented:
Declare int as int? (nullable type) and you should be good. Also you can use int.TryParse method rather than Int32.Parse
0
SriVaddadiCommented:
I mean instead of declaring

int i ..

declare it as int? i
0
lucky_jamesCommented:
int a = ?: -1;

can be translated to    

int a = -1;
if(row["s_id"] ! = null)
{
 a = Int32.Parse(row["s_id"].ToString());
}


-> better use DBNull.Value in place of null I had used. Thanks om_prakash_p for correcting me.
-> more about dbnull class. check out: http://msdn.microsoft.com/en-us/library/system.dbnull.aspx
0
milindsmCommented:
Use this method,

bool Int32.TryParse (string s,       out int result )
0
SriVaddadiCommented:
Try this

for (int i = 0; i < num_R_fuzzy_reuslt; i++)
     {
       DataRow row = DT_fuzzy_reuslt.Rows[i];

     int? a =  Convert.ToInt32(row["s_id"])

 }
0
rmtogetherAuthor Commented:
hi, lucky_james

I would like to learn how to use int a = ?: -1 as well

could you please show me to to do it in your last code. thank you

int a = -1;
if(row["s_id"] ! = null)
{
 a = Int32.Parse(row["s_id"].ToString());
}
0
milindsmCommented:
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

0
lucky_jamesCommented:
>>I would like to learn how to use int a = ?: -1 as well
its a ternary operator:

which says a = logical_expression?c:d
can be translated as
if(logical_expression)
{
a = c;
}
else
{
a = d;
}

check out:
http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx
0
lucky_jamesCommented:
also a = ?: -1 was having a typo mistake.

read it as a = (row["s_id"] ! = DBNull.Value)? Int32.Parse(row["s_id"].ToString()) : -1;

thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.