Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has textboxes on it. I am using this format for the textboxes:

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


I also have a Textbox textchange in place to do some calculations. When it is time for the calculations to do what it does the user gets this error:

HTML
Input string was not in a correct format.


Here is the calculation code for one of the textbox text change:

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox1.Text);
        int j = 12;
        TextBox5.Text = Convert.ToString(i / j);
    }


How can I correct this error to do the calculations?
Posted
Comments
Sergey Alexandrovich Kryukov 6-Oct-14 15:39pm    
Why doing so?! Let's say, input text is "42". Then you will produce "42:42,42". Why? Did you ever here the concepts if informatics, such as entropy?
Besides, you repeat 3 identical lines of code. It should be a call to some method with text box reference used as a parameter. Repeating line of code is opposite to programming.

Also, avoid using convert in such cases. What you do is parsing, so use int.Parse, int.TryParse instead, and the like.

—SA

1 solution

Since you're parsing the string values to integer values (or double as in the String.Format() block). You need to know that 'a' cannot be converted to an integer. Whereas '0' can be.

What I mean is, that the string contains some characters that are not valid numbers. You need to make sure that the characters are numbers.

Quote:
Input string was not in a correct format.


Above statements tells it all, you need to make sure that the characters are actually numbers and can be converted to double or integers respectively.

On MSDN, a quick Google resulted in this link, http://msdn.microsoft.com/en-us/library/bb397679.aspx[^] where it has been made clear that while converting String to other formats like Integer, Double etc, you need to be carefull because String can contain alphanumeric content, whereas double, int, float are restricted to numbers only.

Only way to correct this, is to remove all of the characters from the textboxes that are not numbers, even whitespaces (use .Trim() on the string to remove extra spaces at start or end).

Since you're using ASP.NET, in ASP.NET there is a function used to determine whether the string is an int or not, you can use as

C#
if(variableName.IsInt()) {
   // yes, the variable has int values
   // parse them, convert them, use them
} else {
   // prompt for a valid data
}


You can try out many other ways, but remember, always use if else block to check for small conditions like this one.
 
Share this answer
 
Comments
Computer Wiz99 6-Oct-14 15:07pm    
Afzaal Ahmad Zeehan, Thanks for the post. Where would I put the use.Trim() in my code to get just the numbers?
Afzaal Ahmad Zeeshan 6-Oct-14 15:13pm    
.Trim() method is used on strings, to remove extra whitespaces at the start or end. Whereas the second method can be used by you to check whether it is a number or not. Make sure the user enters the numbers. It won't be a good idea to remove the content from the data user has provided.
Computer Wiz99 6-Oct-14 15:29pm    
So do I put the .Trim() method on the calculation code or the formatted code?
Afzaal Ahmad Zeeshan 6-Oct-14 15:30pm    
As I have already said, don't use Trim to remove alphabets. Use the IsInt() method in the if else block and use the parser inside it.
Computer Wiz99 6-Oct-14 15:34pm    
Okay, I got the don't use .Trim() method. Can you show an example of the IsInt() method?

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