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

I want to convert Label text into Integer datatype.

I tried by giving Convert.Int32(Label1.Text) and int.Parse(Label1.Text)
but I couldn't do Type casting . I am getting error at runtime.

I searched in web I couldn't get the solution.

Please help me....
Posted

Use int.TryParse()


Here is a sample code:
C#
string text = Label1.Text; // Say, "10000";
       int number;
       if (int.TryParse(text, out number))
       {
           // It was a number and it has been already assigned to the variable "<code>number</code>"
       }
       else
       {
           //It was not actually a number
       }

<br />
int.TryParse()
is a better way because it won't throw any exception at runtime if the string value is not actually a number.
 
Share this answer
 
Comments
Dalek Dave 16-Sep-10 7:44am    
Good Answer.
Label1.Text="22";
int number = Convert.Int32(Label1.Text)
number=22;
works fine for me..
what error are you getting
 
Share this answer
 
VB
If e.Row.RowIndex <> -1 Then
Dim salary As Integer = CInt(DataBinder.Eval(e.Row.DataItem, "Salary"))
Dim da As Double = Integer.Parse(salary) * (10 / 100)
e.Row.Cells(e.Row.Cells.Count - 2).Text = da.ToString()
end If
 
Share this answer
 

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