saba awan
saba awan

Reputation: 75

Input string was not in the correct format : Unhandled exception

I want to calculate sum of five textboxes in 1 text box.... But when ever I remain empty one text box out of five textboxes it is giving an error Input string was not in the correct format below is my code

        if (textBox6.Text.Length == 0)

        // textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        {
            textBox11.Text = (Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox7.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox8.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox9.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();
        }
        else if (textBox10.Text.Length == 0)
        {
            //textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();

        }
        else
            textBox11.Text = (Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text) + Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)).ToString();
            textBox12.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox11.Text)).ToString();



       // (Convert.ToInt32(textBox6.Text) +
    }

Upvotes: 0

Views: 3319

Answers (4)

Sayse
Sayse

Reputation: 43300

your code is going to get extremely messy as it goes.. I recommend you refactor..

List<string> texts = new List<string>{textbox1.Text, textbox2.Text};

int sum = 0;
foreach(string t in texts)
{
    int parse = 0;
    if(!int.TryParse(t, out parse))
        //Not a valid number
    sum += parse;   
}
textbox11.Text = sum.ToString();

You could modify this to be from a list of textboxes if you wish to tell the user which textbox is empty


If you are adding every textbox on the page you can just replace the list with

var texts = this.Controls.OfType<TextBox>().Select(tb => tb.Text);

Upvotes: 5

Charlie
Charlie

Reputation: 4895

i'll like to add some points

1) Use String.IsNullorEmpty(textBox1.Text)in programming practice ,although using length is not a bad idea.

2) allow only numeric characters in textbox using properties.

3) Use trim method to fix the string before parsing it .

    String abc=textbox1.Text.trim();

4) Don't compare strings in textbox while debugging, use lengths to compare.

Upvotes: 0

Rangesh
Rangesh

Reputation: 728

Use Int.Tryparse(),when you want to get Integer from textbox.It must be something like this,

string text = "x";
    int num1;
    bool res = int.TryParse(text, out num1);
    if (res == false)
    {
        // String is not a number.
    }

Upvotes: 0

Usman
Usman

Reputation: 3278

try a trick as

  • Check first whether textbox has value or not, if not then replace it with "0" as follows

    textBox6.Text == "" ? 0 : Convert.ToInt32(textBox6.Text)
    

Upvotes: 0

Related Questions