Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There's no error in the code.
But When I run my web site, I get an error:"Input string was not in a correct format."
Please help me ..

What I have tried:

C#
if(LabelPrices.Text.Length > 0 && txtQuantity.Text.Length > 0)
{
int Quantity = Convert.ToInt32(txtQuantity.Text);
decimal Prices = Convert.ToDecimal(LabelPrices.Text);
decimal sum = Prices * Quantity;
decimal TotalSum = sum;
lblTotalPrice.Text = string.Format("{0:N0}", TotalSum);
lblMessageMoreThanStock.Visible = false;
                Lblonyx.Visible = false;
                lblcheck.Visible = false;
                Image3.Visible = false;
                HyperLinkTracking.Visible = false;
                imgleon1.Visible = true;
                imgleon2.Visible = true;
                imgleon3.Visible = true;
                imgleon4.Visible = true;
                lblstaryu.Visible = true;
                lblstarmie.Visible = true;
                lblstarwa.Visible = true;
                lblomanty.Visible = true;
                ImgThumb.Visible = true;
            }
            else
            {
                lblMessageMoreThanStock.Visible = true;
                Lblonyx.Visible = false;
                lblcheck.Visible = false;
                Image3.Visible = false;
                HyperLinkTracking.Visible = false;
                imgleon1.Visible = true;
                imgleon2.Visible = true;
                imgleon3.Visible = true;
                imgleon4.Visible = true;
                lblstaryu.Visible = true;
                lblstarmie.Visible = true;
                lblstarwa.Visible = true;
                lblomanty.Visible = true;
                ImgThumb.Visible = true;
                lblMessageMoreThanStock.Text = "*Should not be more than Available Stock";
txtQuantity.Text = "1";
                lblTotalPrice.Text = LabelPrices.Text;
            }
        }
Posted
Updated 23-Jul-17 6:42am
v2
Comments
RickZeeland 23-Jul-17 12:21pm    
So are you using ASP.NET ? what do you see if you place a breakpoint on the line:
lblTotalPrice.Text = string.Format("{0:N0}", TotalSum);
and hover with your mouse over TotalSum.

1 solution

I see two errors: you are using Convert.ToInt32 and Convert.ToDecimal on textbox strings, without any regard to whether they contain valid numbers. You should be using TryParse in order to catch any invalid characters in the input text.
 
Share this answer
 
Comments
Atlapure Ambrish 24-Jul-17 2:06am    
Another problem I see in the code is use of unnecessary variable "decimal TotalSum = sum;", instead of defining a new variable, you can use 'sum' variable.
Richard MacCutchan 24-Jul-17 3:02am    
That's not exactly a problem, just down to a lack of experience.
Atlapure Ambrish 24-Jul-17 3:13am    
Yeah, its not a problem but definitely a improvement!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900