none
Converting label to int - not working RRS feed

  • Question

  • Hi,

    I am trying to convert a label to int, but i get an error saying:

    "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"
    "Additional Information: The entry string had invalid format."

    What could be wrong here?

     
     private void button1_Click(object sender, EventArgs e)
            {
    
                string input = textBox1.Text;
    
                int score;
    
                score = Convert.ToInt32(this.label1.Text);
               
    
               
    
                if(input == "test")
                {
                     score += 1;
                }
    
            }


    Sunday, January 21, 2018 12:51 AM

Answers


  • I am trying to convert a label to int, but i get an error saying:

    "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"
    "Additional Information: The entry string had invalid format."

     
     private void button1_Click(object sender, EventArgs e)
            {
    
                string input = textBox1.Text;
    
                int score;
    
                score = Convert.ToInt32(this.label1.Text);
               
    

    If the text string has non-numeric characters or is empty you will get
    that exception. Use TryParse instead:

    bool ret = Int32.TryParse(this.label1.Text, out score);

    The return can be tested to see if the conversion succeeded or not.

    - Wayne


    • Proposed as answer by KareninstructorMVP, Moderator Sunday, January 21, 2018 2:17 AM
    • Marked as answer by 2021_ Wednesday, February 27, 2019 3:06 AM
    • Unmarked as answer by 2021_ Wednesday, February 27, 2019 3:06 AM
    • Marked as answer by 2021_ Wednesday, February 27, 2019 3:20 AM
    • Edited by WayneAKing Wednesday, February 27, 2019 6:59 AM
    Sunday, January 21, 2018 1:20 AM

All replies


  • I am trying to convert a label to int, but i get an error saying:

    "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"
    "Additional Information: The entry string had invalid format."

     
     private void button1_Click(object sender, EventArgs e)
            {
    
                string input = textBox1.Text;
    
                int score;
    
                score = Convert.ToInt32(this.label1.Text);
               
    

    If the text string has non-numeric characters or is empty you will get
    that exception. Use TryParse instead:

    bool ret = Int32.TryParse(this.label1.Text, out score);

    The return can be tested to see if the conversion succeeded or not.

    - Wayne


    • Proposed as answer by KareninstructorMVP, Moderator Sunday, January 21, 2018 2:17 AM
    • Marked as answer by 2021_ Wednesday, February 27, 2019 3:06 AM
    • Unmarked as answer by 2021_ Wednesday, February 27, 2019 3:06 AM
    • Marked as answer by 2021_ Wednesday, February 27, 2019 3:20 AM
    • Edited by WayneAKing Wednesday, February 27, 2019 6:59 AM
    Sunday, January 21, 2018 1:20 AM
  • Hello 2021_,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:21 PM
    Moderator