0

I have been through several posts of the same error, and I cant figure it out why it wont work for me.

I am trying to add multiple values in textboxes. Im starting with 2 textboxes to test, but my plan is to have a total of 10. But i keep getting this error. I have this event on each textbox. Ideas? IF needed, i can include my asp code.

protected void calculateLabs(object sender, EventArgs e)
    {
        string lab01 = TextBox1.Text;
        string lab02 = TextBox2.Text;

        int num1 = int.Parse(lab01);
        int num2 = int.Parse(lab02);

        int final = num1 + num2;

        Label1.Text = final.ToString();
    }

UPDATE, it works, but the exception still displays the message on the label, once i input the next values, the error goes away.

protected void calculateLabs(object sender, EventArgs e)
{
    try
    {
        int num1 = Convert.ToInt32(TextBox1.Text);
        int num2 = Convert.ToInt32(TextBox2.Text);

        int final = num1 + num2;

        Label1.Text = final.ToString();
    }
    catch(Exception ex)
    {
        Label1.Text = ex.Message;
    }
}
Onlytito
  • 167
  • 1
  • 2
  • 17

3 Answers3

0
  • Make sure the entered strings in both Textboxes are numbers. Refer to this question to do so: How do I make a textbox that only accepts numbers?
  • Try Convert.ToInt32(string s) instead of Int.Parse(string s)
  • Add a try-catch block and tell us where exactly is the exception thrown.

UPDATE:

Before converting values of Textbox1 & Textbox2 make sure they have values and not euqual null

    protected void calculateLabs(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(TextBox2.Text))
            {
                int num1 = Convert.ToInt32(TextBox1.Text);
                int num2 = Convert.ToInt32(TextBox2.Text);
                int final = num1 + num2;
                Label1.Text = final.ToString();
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
Community
  • 1
  • 1
Ahmed Elazazy
  • 454
  • 4
  • 10
  • I added the exception and used convert method, and it have the error display in the label. It does say the same error, but then when i continue, it adds the values. I updated my question with the exception code. – Onlytito Mar 29 '15 at 15:04
  • You are setting the Label to the message of the exception: "Label1.Text = ex.Message", You'd better log it somewhere else. – Ahmed Elazazy Mar 29 '15 at 15:13
0

Why are you using the word final as a variable name? It is a reserved word for c# and other languages try using other variable names like total, finalNum, etc., instead.

Jethro Monzada
  • 104
  • 1
  • 9
  • I tried changing the variable name, and still gives me an error. I set all values in my textboxes to 0 and it worked. – Onlytito Mar 29 '15 at 17:16
0

Not sure why, i keep getting the error. So i just included where the default value of each textbox is 0.

 <asp:TextBox ID="part1" runat="server" AutoPostBack="true" OnTextChanged="calculateSum" Text="0">

Doesnt error out anymore.

Onlytito
  • 167
  • 1
  • 2
  • 17