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?
 
Om PrakashCommented:
Try
int a = 0
if (row["s_id"] != DBNull.Value)
{
a = Int32.Parse(row["s_id"].ToString());  
}
0
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
rmtogetherAuthor Commented:
is it use if statement?
could you transfer to regular if statement. I have little confuse about it.
thank you
0
 
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
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.