Link to home
Start Free TrialLog in
Avatar of datzent83
datzent83Flag for United States of America

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.LiteralShipping.Text) - ((this.intlShipping.Checked)?intlShippingAmount:0)) + " S&H";
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 

Open in new window

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

this typically occurs when you pass an empty string to Parse() method.
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";

ASKER CERTIFIED SOLUTION
Avatar of Nate Feinberg
Nate Feinberg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ROMA CHAUHAN
your LiteralShipping.Text must be blank. and you can not convert "" to double thats why its giving this error