Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all.

I have a form that has textboxes on it. Each textbox has a TextChanged on it to do some calculations. I have a Regular Expression Validator in place to check on if the user is entering a character instead of a number. If a user enters 10525.5 it is suppose to recalculate to 10,526 but I get this error:

HTML
Input string was not in a correct format.


Why am I getting this error and what is the best way to correct it?

Here is my HTML code for the textboxes:

ASP.NET
<pre><asp:TextBox ID="TextBox1" runat="server" Width="180px" 
                    ToolTip="IPEDS Part C, Line 01, Column 01" AutoPostBack="True" 
                    ontextchanged="TextBox1_TextChanged" ValidationGroup="Check">0</asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator14" 
                    runat="server" ControlToValidate="TextBox1" CssClass="style18" 
                    ErrorMessage="You Must Enter Instruction" ForeColor="Red" 
                    ValidationExpression="^[-]?\d+$." ValidationGroup="Check"></asp:RegularExpressionValidator>




Here is the C# code for the calculations and adding and removing commas:

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBox4.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBox5.Text.Replace(",", ""));
            int g = Convert.ToInt32(TextBox6.Text.Replace(",", ""));
            
            int j = Convert.ToInt32(TextBox7.Text.Replace(",", ""));
            int k = Convert.ToInt32(TextBox8.Text.Replace(",", ""));
            int l = Convert.ToInt32(TextBox9.Text.Replace(",", ""));
            int m = Convert.ToInt32(TextBox10.Text.Replace(",", ""));
            TextBox88.Text = Convert.ToString(a + b + c + d + f + g + j);
            TextBox89.Text = Convert.ToString(a + b + c + d + f + g + j + k + l + m);

            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox2.Focus();


The Source Error is at:

C#
int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));


What I have tried:

I have tried to Comment out the calculation area and the rounding and number converted works.

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox2.Focus();
        }
Posted
Updated 15-May-18 8:47am
v2

int a = Convert.ToInt32(TextBox1.Text.Replace(",", ""));


Whatever TextBox1.Text.Replace(",", "") returns can't be converted to an int. Remember your validators are just that, they validate. You can ask "is this control valid?", it doesn't stop a user from entering invalid characters into that box and it doesn't remove invalid characters from it either. It simply lets you check if the controls are valid before you process them. So in your change event use the validator to see if the controls are valid and if not show an error rather than running your code.

Also use int.TryParse rather than Convert.ToInt32.
 
Share this answer
 
Comments
Computer Wiz99 15-May-18 9:04am    
Hello F-ES Sitecore. Thanks for the info. The TextBox1.Text.Replace(",", "") code is for removing the commas that the system adds in when the when the focus is set to another textbox so the calculations will fire. What I was trying to do is to have it to where the user can not enter nothing else but numbers. It works on one textbox but I get the error I posted when the textbox that does the calculations fires. So if I enter 10500 it turns to 10,500 no problem and the calculations fire. When I enter 10500.69 it gives me the error that I posted. I trying to get it to where if the user enters a number like 10500.69 that the code will fire correctly, do the calculations and correct the number to 10,501.
I have the solution!!! I got it to work!!!

C#
protected void TextBoxInstr_TextChanged(object sender, EventArgs e)
        {

            int a = Convert.ToInt32(TextBox1.Text.Replace(",", "").Replace(".", ""));
            int b = Convert.ToInt32(TextBox2.Text.Replace(",", "").Replace(".", ""));
            int c = Convert.ToInt32(TextBox3.Text.Replace(",", "").Replace(".", ""));
            int d = Convert.ToInt32(TextBox4.Text.Replace(",", "").Replace(".", ""));
            int f = Convert.ToInt32(TextBox5.Text.Replace(",", "").Replace(".", ""));
            int g = Convert.ToInt32(TextBox6.Text.Replace(",", "").Replace(".", ""));
            
            int j = Convert.ToInt32(TextBox7.Text.Replace(",", "").Replace(".", ""));
            int k = Convert.ToInt32(TextBox8.Text.Replace(",", "").Replace(".", ""));
            int l = Convert.ToInt32(TextBox9.Text.Replace(",", "").Replace(".", ""));
            int m = Convert.ToInt32(TextBox10.Text.Replace(",", "").Replace(".", ""));
            TextBox88.Text = Convert.ToString(a + b + c + d + f + g + j);
            TextBox89.Text = Convert.ToString(a + b + c + d + f + g + j + k + l + m);


            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox2.Focus();
        }


I added
C#
.Replace(".", "")
to all of the calculations and it worked.
 
Share this answer
 
Comments
Richard Deeming 15-May-18 15:05pm    
That's not going to work.

If the user enters "123.45", you'll treat it as "12345". But your question says you want to treat it as "123".

If the user enters "Boo!", you'll get an exception.

You'll want to convert to a double first, using double.TryParse[^] to avoid errors for invalid input. Then use Math.Round to round the value, and cast it to an int.

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