Hello,
I think that's because you are using '=' operator for employee name instead of 'LIKE'. '=' operator means that you are looking for exact match. 'LIKE' operator allows you to search using wildcards (partial searching). Try code below:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SUBHAN-PC;Initial Catalog=subhan;Integrated Security=True");
DataTable dt = new DataTable();
string query = "SELECT * FROM ghtdetails WHERE EmployeeName LIKE @EmployeeName";
if (chkFilterBySalary.Checked)
{
query = query + " AND Salary >= @SalaryFrom";
query = query + " AND Salary <= @SalaryTo";
}
SqlDataAdapter da = new SqlDataAdapter(query, con);
var command = da.SelectCommand;
command.Parameters.AddWithValue("EmployeeName", string.IsNullOrWhiteSpace(textBox1.Text) ? "%" : textBox1.Text);
if (chkFilterBySalary.Checked)
{
command.Parameters.AddWithValue("SalaryFrom", nudSalaryFrom.Value);
command.Parameters.AddWithValue("SalaryTo", nudSalaryTo.Value);
}
da.Fill(dt);
da.ToString();
dataGridView1.DataSource = dt;
}
[Update 1]
1. 'LIKE' operator can be used with STRING type fields only. I'm assuming that 'Salary' is numeric type (single, double or decimal). So, you can use '=', '>', '>=', '<' or '<=' operators only for filtering.
2. You can not have two 'WHERE' keywords in one query. To set more than one filter condition use 'AND' or 'OR' keywords.
3. In my opinion it's better to separate filtering by text and numeric fields. To do that add new controls to your Form: one CheckBox (name it: chkFilterBySalary) and two NumericUpDown controls (name them: nudSalaryFrom and nudSalaryTo) and update your code.
[Update 2]
Added use of parametrized query. Based on Richard Deeming's comment.
I hope it help you.