Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

currently I'm trying to convert string to int and the data I've retrieved from db.
Here is my code:

ant = int.parse(dr[1].tostring());

Please help me.I'm stuck with this bug!.
Posted
Updated 18-Aug-10 23:36pm
v4
Comments
Nuri Ismail 19-Aug-10 4:22am    
You will have to elaborate on what the result string from "dr[1].tostring()" looks like? Is it something like "666" or "Ala Bala Bum" or...
Toli Cuturicu 19-Aug-10 5:35am    
bad syntax, anyway; you should have:
ant = int.Parse(dr[1].ToString());

You should better use TryParse instead:
C#
int ant;
if (int.TryParse(dr[1], out ant)
{
    // everything ok
}
else
{
    // bad format -> take some corrective action!
}
 
Share this answer
 
Comments
Nuri Ismail 19-Aug-10 5:42am    
Reason for my vote of 5
Good answer! :)
Int32.TryParse[^] might be what you are looking for: check the code examples given for usage.
 
Share this answer
 
MissNano

There may be two issues: (hope you have declared ant variable as int)

1. The data returned from the DB might be the Null value. hence error.

2. The data returned from the DB might not be the string value.

However, the String should contain the int value in string format.

Regards
 
Share this answer
 
v3
Comments
Dalek Dave 19-Aug-10 4:33am    
Seems reasonable.
Toli Cuturicu 19-Aug-10 5:38am    
Reason for my vote of 1
Serious misunderstanding of the framework:
int.Parse returns an int, not a string!
Please, don't answer any more questions when you don't know what you are talking about.
shivaprasadk 19-Aug-10 6:09am    
Toli Cuturicu: You are right as far as the ant variable type. It should be int.
hi
you will do the folling code


ant = Convert.ToInt32( dr["columnName"].ToString());
thanks
 
Share this answer
 
Comments
Arun Jacob 19-Aug-10 6:26am    
Reason for my vote of 1
Wrong Answer. Even if you do like this,it'll throw error if dr["ColumnName"] is null, because ToString() doesn't handle null.
Toli Cuturicu 19-Aug-10 10:30am    
Reason for my vote of 1
not quite so

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