开发者

casting, converting, and input from textbox controls

Working on some .aspx.cs code and decided I would forget how to turn a textbox value into a useable integer or decimal. Be warned I'm pretty new to .asp. Wish I could say the same for c sharp. So the value going into my textbox (strawberryp_textbox) is "1" which I presume I can access with the .text property. Which I then parse into a int. The Error reads Format Exception was unhandled by user code.

My other question is can I do operations on a session variable?

protected void submit_order_button_Click(object sender, EventArgs e)
{
    int strawberryp;
    int strawberrys;

    decimal money1 = decimal.Parse(moneybox1.Text);
    decimal money2 = decimal.Parse(moneybox2.Text);
    decimal money3 = decimal.Parse(moneybox3.Text);
    decimal money4 = decimal.Parse(moneybox4.Text);
    decimal money5 = decimal.Parse(moneybox5.Text);

    strawberryp = int.Parse(strawberryp_Textbox.Text);    //THE PROBLEM RIGHT HERE!
    strawberrys = int.Parse(strawberrys_Textbox.Text); // Needs fixed
    int strawberryc = int.Parse(strawberryc_Textbox.Text); //fix
    int berryp = int.Parse(berryp_Textbox.Text); //fix
    int raspberryp = int.Parse(raspberryp_Textbox.Text); /fix
    decimal subtotal = (money1 * strawberryp) + (money2 * strawberrys) + (money3 * strawberryc) + (money4 * berryp) + (money5 * raspberryp); //check to see if you can multiply decimal and int to get a deciaml!!


    Session["pa开发者_开发知识库ssmysubtotal"] = subtotal; //TextBox2.Text;

    (strawberryp_Textbox.Text);//TextBox4.Text;

    add_my_order_button.Enabled = true;
    add_my_order_button.Visible = true;
    submit_order_button.Enabled = false;
    submit_order_button.Visible = false;
    strawberryp_Textbox.ReadOnly = false;
    strawberrys_Textbox.ReadOnly = false;
    strawberryc_Textbox.ReadOnly = false;
    berryp_Textbox.ReadOnly = false;
    raspberryp_Textbox.ReadOnly = false;
    Response.Redirect("reciept.aspx");
}

Thanks for the help


Post the results of putting a Console.WriteLine(strawberryp_Textbox.Text); just before the line in your code that has the "THE PROBLEM..." comment. If you run the app under the Visual Studio web server (usually by debugging - hit F5), the output should show up in the Output pane in Visual Studio. That might help verify that only the single character '1' is in the Text property of that textbox.

Also, a best practice is to use TryParse instead of Parse so you can more carefully control responding to errors in the expected values.


Try decimal.TryParse(...) or int.TryParse(...).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜