简体   繁体   中英

Error on int.parse

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.

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.

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

what does it means?

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

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

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.

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 (eg 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.
    }
}

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM