datzent83
asked on
C#.NET Input string was not in a correct format.
I have the following code but it returns Input string was not in a correct format. I am not srue where the error is.
Session["pp_item_name"] += " +" + (double.Parse(this.Literal Shipping.T ext) - ((this.intlShipping.Checke d)?intlShi ppingAmoun t:0)) + " S&H";
Session["pp_item_name"] += " +" + (double.Parse(this.Literal
Server Error in '/dev/content' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 196: Session["pp_item_name"] += (int.Parse(txtQ3.Text) > 0) ? ", " + txtQ3.Text + "x Bracelet" : "";
Line 197: Session["pp_item_name"] += (int.Parse(txtQ2.Text) > 0) ? ", " + txtQ2.Text + "x Purse" : "";
Line 198: Session["pp_item_name"] += " +" + (double.Parse(this.LiteralShipping.Text) - ((this.intlShipping.Checked)?intlShippingAmount:0)) + " S&H";
Line 199: Session["pp_item_name"] += ((double)Session["discount"] > 0) ? ", $" + Session["discount"].ToString() + " DIS, COUP = " + Session["discountCode"].ToString() : "";
Line 200: Session["pp_item_name"] += (this.intlShipping.Checked) ? " + $" + intlShippingAmount + " Intl Shipping Fee" : "";
Source File: e:\web\myfirstper1\htdocs\dev\content\cart.aspx.cs Line: 198
Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471335
System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) +115
System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info) +192
System.Double.Parse(String s) +23
cart.ConfirmButton_Click(Object sender, EventArgs e) in e:\web\myfirstper1\htdocs\dev\content\cart.aspx.cs:198
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.4016; ASP.NET Version:2.0.50727.4016
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
your LiteralShipping.Text must be blank. and you can not convert "" to double thats why its giving this error
You can use TryParse instead:
replace:
Session["pp_item_name"] += (int.Parse(txtQ3.Text) > 0) ? ", " + txtQ3.Text + "x Bracelet" : "";
with:
result = 0; // declare result (int) somewhere before
if (int.TryParse(txtQ3.Text, out result) && result > 0)
Session["pp_item_name"] += ", " + txtQ3.Text + "x Bracelet";