Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am doing some Rounding and calculations on a Web form. When I debug the program everything else works until I get to the Rounding part. I get and error "Input string was not in a correct format". So I comment out that section and debugged the program again. The Rounding works and displays. When it is trying to add the numbers it throws the Input string was not in a correct format error. What did I do wrong?

Here is all of the code:

C#
protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(TextBoxTHUG.Text.Replace(",", ""));
            //int j = 12;
            TextBoxTHUGDR.Text = Convert.ToString(i / 12.0);

            int a = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));
            TextBoxT1234.Text = Convert.ToString(a + b + c + d);

            int g = Convert.ToInt32(TextBoxT1234.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBoxNCCDR.Text.Replace(",", ""));
            TextBoxTCNC.Text = Convert.ToString(g + f);


            int o = Convert.ToInt32(TextBoxLYTCNC.Text.Replace(",", ""));
            int p = Convert.ToInt32(TextBoxTCNC.Text.Replace(",", ""));
            TextBoxFTE40.Text = Convert.ToString(Math.Round((Math.Abs(p - o) * 100.0 / ((o)))));
            TextBoxFTE40.Text = Math.Round(Convert.ToDouble(TextBoxFTE40.Text), 2).ToString();

            RangeValidatorLYTHUGDR.Validate();
            RangeValidatorLYTCNC.Validate();
            TextBoxTHUGDR.Text = Math.Round(Convert.ToDouble(TextBoxTHUGDR.Text.ToString()), 2).ToString();
            TextBoxTHUGDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUGDR.Text));
            TextBoxTHUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUG.Text));
            TextBoxT1234.Text = string.Format("{0:0,0}", double.Parse(TextBoxT1234.Text));
            TextBoxTCNC.Text = string.Format("{0:0,0}", double.Parse(TextBoxTCNC.Text));
            TextBoxTHG.Focus();
        }


Here is where the error is throwing:

C#
int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));


How can I fix this?
Posted
Comments
Foothill 20-Nov-15 9:56am    
What is the text value of TextBoxTHUGDR?
Computer Wiz99 20-Nov-15 10:10am    
In TextBoxTHUGDR the value is 127 when it is rounded. When I comment out the adding part it works but the other values in the other textboxes does not add the value in TextBoxTHUGDR.
phil.o 20-Nov-15 10:05am    
Please launch a debug session and watch for your variable values.
Dave Kreskowiak 20-Nov-15 10:37am    
You should not be using Convert.ToInt32 in this situation. If any of those textboxes are empty, the code will fail. An empty string does NOT convert to 0! There is no meaningful conversion of an empty string to an integer.
Computer Wiz99 20-Nov-15 10:53am    
What should I use instead of Convert.ToInt32? It has worked in the past. And I have 0 in all the textboxes when the page loads.

1 solution

First off, don't use Convert - use TryParse instead, as it returns a "success / fail" indicator instead of throwing an exception. This allows you to report problems back to your user instead of your program failing.

And that's probably what the problem is: the textbox doesn't contain a valid integer, even after you have removed all the commas. Nothing we can do about that - it's an input error and it's your responsibility to detect those, and get the user to correct his inputs before you continue.
 
Share this answer
 

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



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