We help IT Professionals succeed at work.

C#.NET Input string was not in a correct format.

datzent83
datzent83 asked
on
2,384 Views
Last Modified: 2013-12-17
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

Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
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";

Financial Center Operations Manager
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ROMA CHAUHANProject Lead
CERTIFIED EXPERT

Commented:
your LiteralShipping.Text must be blank. and you can not convert "" to double thats why its giving this error
ROMA CHAUHANProject Lead
CERTIFIED EXPERT

Commented:
This may also help you,

http://www.homeandlearn.co.uk/csharp/csharp_s3p9.html
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/334953a0-e94b-4b46-8621-e8fb034f78c5/
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.