0

Input string was not in a correct format. At this line:

int total = 0;
total = int.Parse(TextBox2.Text) + int.Parse(TextBox4.Text) + int.Parse(TextBox6.Text) + 
        int.Parse(TextBox8.Text) + int.Parse(TextBox10.Text) + int.Parse(TextBox12.Text) + 
        int.Parse(TextBox14.Text) + int.Parse(TextBox16.Text);
Label1.Text = total.ToString(); 

I would like to pass the value to another page. what does it means? T_T Thanks in advance :)

protected void Button1_Click(object sender, EventArgs e)
{
    Session["Month"] = DropDownList2.SelectedValue;
    Session["expen1"] = TextBox1.Text;
    Session["expen2"] = TextBox3.Text;
    Session["expen3"] = TextBox5.Text;
    Session["expen4"] = TextBox7.Text;
    Session["expen5"] = TextBox9.Text;
    Session["expen6"] = TextBox11.Text;
    Session["expen7"] = TextBox13.Text;
    Session["expen8"] = TextBox15.Text;

    int totalvalue = 0;
    totalvalue = int.Parse(TextBox2.Text) + int.Parse(TextBox4.Text) + int.Parse(TextBox6.Text) + int.Parse(TextBox8.Text) + int.Parse(TextBox10.Text) + int.Parse(TextBox12.Text) + int.Parse(TextBox14.Text) + int.Parse(TextBox16.Text);
    Label1.Text = totalvalue.ToString(); 

    Session["price1"] = TextBox2.Text;
    Session["price2"] = TextBox4.Text;
    Session["price3"] = TextBox6.Text;
    Session["price4"] = TextBox8.Text;
    Session["price5"] = TextBox10.Text;
    Session["price6"] = TextBox12.Text;
    Session["price7"] = TextBox14.Text;
    Session["price8"] = TextBox16.Text;
    Session["total"] = Label1.Text;

    Server.Transfer("sum.aspx");


}

I want to store the result in sum.aspx.

2
  • It means one of the text box values wasn't a number that could be parsed into an int. Try examining what each of the TextBox.Text values are and you'll find the culprit. Commented Feb 23, 2012 at 9:08
  • 2
    instead of using textboxes and parsing text you trust to be numeric, use some sort of input mask or validation BEFORE parsing. Alternatively, use a different control like a Numeric up/down or numeric spinner. Commented Feb 23, 2012 at 9:11

8 Answers 8

2

If any of your TextBox values are null or are not a number, this will break. In order for this to work, all of the TextBox values will need to have a default value of 0 and you will have to restrict the input of the TextBox to numbers.

Sign up to request clarification or add additional context in comments.

3 Comments

Looking at your code, you could use a ternary operator - Session["expen1"] = String.IsNullOrEmpty(TextBox1.Text) ? "0" : TextBox1.Text;
Thank you neil. You such a saver
I have just seen an issue. If you've implemented the ternary operator suggestion, then in your total = TextBox1 etc. line, you'll need to use the Session variables, so your line will look like: total = int.Parse(Session["expen1"].ToString()) + etc.
1

Instead of using textboxes and parsing text you trust to be numeric, use some sort of input mask or validation BEFORE parsing. Alternatively, use a different control like a Numeric up/down or numeric spinner.

You need to learn about how to handle exceptions, when to use try parse and when to use parse...

Comments

0

If any of the textboxes is empty, you will get an exception, since empty text cannot be parsed. Use int.TryParse instead.

1 Comment

i see. that means, all textbox should has the value? but how if the user only enter value in some of the textbox? The 8 textboxes are not compulsory to be entered. Do get me?
0

what does it means?

One of your TextBoxes contains a text which can't be parsed as an Integer.

Comments

0

Check Each textbox data should be numbers. if try enter string and validating with int.parse you will get this error.

Comments

0

It means that one of the textboxes values (TextBox#.Text) contains a value that cannot be "converted" to an integer.

What values are inside the textboxes? For example, if the textbox contains a non-numeric character it wont be able to convert, since the letter 'a' has no numeric value.

Comments

0

It means that one of the calls to int.Parse threw an exception because the text value was not a value that could be parsed into an Integer (e.g. the text was a non numeric value).

A better way to do this would be:

var textBoxesToParse = new [] { TextBox2, TextBox4, TextBox6, TextBox8, TextBox10, TextBox12, TextBox14, TextBox16 };

int total = 0;

foreach (var textBox in textBoxesToParse)
{
    int textBoxValue;

    if(int.TryParse(textBox.Text, out textBoxValue))
    {
        total += textBoxValue;
    }
    else
    {
        // The textbox had an invalid value, up to you what you need to do here.
    }
}

Comments

0

As has been mentioned, the error is that one of your textboxes has either a blank or a non-numeric value.

You can use a RegularExpressionValidator so that the user is permitted to submit the form only when the values are numeric.

<asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server"    
 ControlToValidate="txtLastName"Display="Dynamic" ErrorMessage="Numeric characters only"
 ForeColor="Red" ValidationExpression="^[0-9]*$"

Additionally, you should also look to use tryParse or Int32.Parse(); the latter returns 0 if it is passed a null string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.