Click here to Skip to main content
15,887,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int a = 0;
a=Convert.ToInt32(Label12.Text.ToString());//error

Label12.text has some values in it..

error:

Input String was not in correct format...

What should i do to solve this error???
Posted
Updated 25-Feb-13 19:43pm
v3
Comments
Nandakishore G N 26-Feb-13 1:42am    
verify whether the label contains integer or any string values?
Sergey Alexandrovich Kryukov 26-Feb-13 1:53am    
Why? :-)
—SA
bbirajdar 26-Feb-13 1:48am    
What do you means by "some values in it.." ?????? Are you trying to convert a user's name to int ?
Sergey Alexandrovich Kryukov 26-Feb-13 1:53am    
OP's users get names "1", "2", "3"... :-)
—SA
Sergey Alexandrovich Kryukov 26-Feb-13 1:54am    
No, you should not fix the bug. This is not a bug, the whole idea makes no sense.
—SA

You cannot directly convert a string to an integer.
Use Interger.TryParse to avoid errors during conversion.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-13 2:14am    
Sure, a 5. Actually, Convert is supposed to do the same (and throw exception), but Parse or TryParse (depending on the purpose) is always better, because the name shows what really happen. "Convert" is a pretty bad word.
—SA
Abhinav S 26-Feb-13 3:18am    
Thank you.
Because the text of the label is not in valid format, cannot be parsed as integer. More importantly, the whole thing makes no sense. Label is a static control (text is not modified by the user), so why parsing it, ever? Makes no sense at all.

—SA
 
Share this answer
 
Comments
Abhinav S 26-Feb-13 2:05am    
5. I suggested TryParse.
Sergey Alexandrovich Kryukov 26-Feb-13 2:12am    
Thank you, Abhinav. Right, one could use TryParse, or Parse would need try-catch block (then why?), but only if the whole thing of parsing makes sense. If this case, it does not. :-)
—SA

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