Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (txt_search.Text == "" || int.Parse(txt_salary.Text)<=10000) { string display = "Enter the Employee ID to Complete Search"; ClientScript.RegisterStartupScript(this.GetType(), "Empty Text Box", "alert('" + display + "');", true); }

can anyone explain how can i remove this error i want to search employee having salary greater than 10000 when i press search button there was a error input string was not in a correct format.

What I have tried:

I have tried parse method to convert string
Posted
Updated 30-Jul-18 19:17pm
Comments
Member 13931382 30-Jul-18 14:09pm    
I want to make employee management system and search only those employees records whose salary is greater than 10000
CHill60 30-Jul-18 14:38pm    
What is in txt_Salary.Text?
Member 13931382 30-Jul-18 14:40pm    
Employee salary
CHill60 30-Jul-18 14:44pm    
No - what value is in txt_Salary.Text when you get the error?
Member 13931382 30-Jul-18 14:48pm    
according given condition I entered less than 10000 but an error message print on screen input string was not in a correct format

1 solution

The error message you get says "The string you tried to convert does not contain a number, so I can't convert it" - and that means that whatever the user has typed into txt_salary.Text isn't just digits - it may be "12 34", or "Hello", or "$10,000" , or perhaps "10000.00"

Instead of using int.Parse, at the top of the method use int.TryParse instead:
C#
int salary;
if !(int.TryParse(txt_salary.Text, out salary))
    {
    ... Bad input by user, report it to him ...
    return;
    }
And also validate all other entry fields. You can then use the validated values in salary and such line confident that they will work.
 
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