Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a form that Divides, Rounds and Adds. I got the Dividing and Rounding working but when trying to Add I get this error:

C#
Input string was not in a correct format.


On this line:

C#
int d = Convert.ToInt32(TextBoxTHGDR.Text.Replace(",", ""));


Here is the whole code behind:

C#
protected void TextBoxTHG_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(TextBoxTHG.Text.Replace(",", ""));
            TextBoxTHGDR.Text = Convert.ToString(i / 9.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);
            TextBoxTHGDR.Text = Math.Round(Convert.ToDouble(TextBoxTHGDR.Text), 2).ToString();
            TextBoxTHGDR.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHGDR.Text));
}


What is going on is that when a number is entered it is Divided, Rounded and Added with other numbers. How can I stop this error from happening? What did I miss?
Posted
Updated 7-Jan-16 6:14am
v2

First off, stop trying to use Convert.Int32 to deal with user input: use int.TryParse instead. It has an overload which allows thousands separators as the NumberStyles value:
https://msdn.microsoft.com/en-us/library/zf50za27%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396[^]
 
Share this answer
 
The exception means that the string you're trying to parse an integer from doesn't actually contain a valid integer.

The issue may be due to following reasons:

It may contains extra space. Use trim() function to delete unnecessary sapces.
String is not correct format means either it contains some alphanumeric, chars or special chars.

When your string should conatin only value from 0-9 except that it throw you exception.

You can use int.TryParse(). However if exception occurs you will get 0 as result.
C#
int finalResult;
bool output;
string str = "";
output = int.TryParse(str, out finalResult); // Here you wil get 0 as result
 
Share this answer
 
Comments
Computer Wiz99 7-Jan-16 12:38pm    
Okay. I debugged it. Just letting it Divide and Round. I get the correct answer but it will not Add to the other numbers after the Rounding. No other charter is in the textbox. What now?
[no name] 7-Jan-16 12:41pm    
What is the textbox value you are getting when debugging.
Computer Wiz99 7-Jan-16 12:48pm    
When I enter 2204 I one textbox I get 245 in TextBoxTHGDR. Which is correct. But the other code that Adds each value when ever the value changes is throwing the error.

This code works:
int i = Convert.ToInt32(TextBoxTHG.Text.Replace(",", ""));
TextBoxTHGDR.Text = Convert.ToString(i / 9.0);

This code is throwing the error but at line d:
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);

Computer Wiz99 7-Jan-16 13:43pm    
Where should I put the Trim()?

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