Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys! i am getting an error when i am compare two textbox values in c# \
the error is input string was not in correct format
please help me and tell me the solution

What I have tried:

C#
if (int.TryParse(textBox00.Text) > int.TryParse(txtTotal.Text))
                        {
                            MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
Posted
Updated 19-Dec-16 13:48pm
Comments
cvogt61457 19-Dec-16 18:58pm    
TryParse() returns a bool whether the value was successfully parsed. Not an integervalue.
To get the value, you need to provide a output variable or use int.Parse().
If you use int.Parse(), you will need to verify that the value does parse correctly.
I'm curious how you got this compile.
Philippe Mori 19-Dec-16 19:08pm    
If you got that error, then you are probably using int.Parse and the input is not valid. Please post the real code you are using. Also, if you get an error, you should also indicate the content of the text boxes.

Another thing is that you should give your controls meaningful names. textBox00 is not very informative.

And given the fact that a text box could be empty or contain something that is not a number, you usually want to do the validation in multiple steps so that you can display the most appropriate error to the user.

Hi Member 9983063,

This is not the way TryParse() works. TryParse() requires two parameters - one is the text to convert to integer, and the second is the output variable where it stores the converted value. The code you provided translates into:
C#
if (Boolean > Boolean)     // You are trying to compare booleans.
{
    MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}
This itself is wrong as a boolean can be compared with another boolean for '==' and '!=' only. Logically you cannot say 'if true is greater than false'. So this is one more compilation error along with the other compile error for TryParse().

You can try the following approach:
C#
int textBox00Val, txtTotalVal;
if (int.TryParse(textBox00.Text, out textBox00Val) == false)    // This line checks if there is an integer in the box, and also converts it to an integer.
{
    MessageBox.Show("textBox00 value must be anumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

if (int.TryParse(txtTotal.Text, out txtTotalVal) == false)      // This line checks if there is an integer in the box, and also converts it to an integer.
{
    MessageBox.Show("txtTotal value must be anumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

if (textBox00Val > txtTotalVal)     // Now you need to compare the integers.
{
    MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}
............
............
What happens here is, you obtain the values in integers and then compare the integers.

@cvogt61457: I guess Member 9983063 meant a compile error, as the code in question will never compile (TryParse() must be provided two parameters, and a boolean cannot be compared to another boolean for 'greater than' operator).
 
Share this answer
 
Comments
Member 9983063 19-Dec-16 19:44pm    
what is the mean by must be a number bcz in my project user just can write only integer but it's show must be a number
Mehedi Shams 19-Dec-16 19:57pm    
Hi Member 9983063, it is just a message; please modify as suits. Number and integer are meant for the same.
Karthik_Mahalingam 31-Jan-17 2:08am    
5
Hi try this

C#
 int textBox00 = 0;
 int txtTotal = 0;

if(int.TryParse(textBox00.Text, out textBox00) && int.TryParse(txtTotal.Text,out txtTotal))
 {
     if (textBox00 > txtTotal)
     {
         MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }else
     {

     }
 }else
 {
     //Conversion error
     MessageBox.Show("no valid number found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

 }
 
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