Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert a String value to a Int value using convert function in C#. Can anybody help me in this regards...

String A = dt.Rows[0]["Quno"].ToString();
Int B =Int32.Parse (A);
Posted
Comments
Hiren solanki 1-Oct-10 4:45am    
can you please debug and tell us what you are getting in A ?
Hiren solanki 1-Oct-10 4:45am    
furthermore, you can have debugging by putting breakpoint.
MalikRizwan 3-Oct-10 1:28am    
You better debug your code. it could be because of ANYTHING.. probably your data being fetched.

Hi,
Please use following, if dt.Rows[0]["Quno"].ToString(); contain null value then it will automatically handle and it will not gives error

C#
if(dt != null && dt.Rows.Count > 0)
        {
        Int32 B = Convert.ToInt32(dt.Rows[0]["Quno"].ToString());
        }


Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
Comments
Sandeep Mewara 1-Oct-10 11:41am    
But handling just an error should not be the solution. Root cause should be handled and dealt with. What say?
MalikRizwan 3-Oct-10 1:23am    
you code will throw exception IF dt==null becuase you are checking Count in same condition where you are checking null. if dt is null you can't check Row.Count
hance_micle 1-Dec-10 6:24am    
Good call!!
This is because the string is blank or contains characters not valid for a number

I would recommend using TryParse
int iVal;
if (int.TryParse(myString, out iVal))
   // this worked
else
   // this failed
 
Share this answer
 
v2
Comments
MalikRizwan 3-Oct-10 1:25am    
int.TryParse doesn't accept second parameter as reference.. it should be out paramter.

int.TryParse(myString,out iVal);
Use int.TryParse rather than Int32.Parse. Confirm you are entering a valid string.
 
Share this answer
 
v2
Int32 i = (dt.Rows[0]["Quno"].ToString() == null) ? 0 : Convert.ToInt32(dt.Rows[0]["Quno"].ToString());
 
Share this answer
 
Comments
Sandeep Mewara 1-Oct-10 11:42am    
If 'dt.Rows[0]["Quno"]' is NULL then? dt.Rows[0]["Quno"].ToString() would bomb!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900