SQL SELECT - filtering
I have a form that allows the client to seach through the record depending on a number on two different search options. The user also has the option of searching by only using either the text field or the dropdown list.
The client can search by either entering 'keywords' in the textfield or selecting from a dropdownlist.
The drop down list has a option 'Any'.
If there user selects 'Any' then i need that filter to be ignored.
Here is the code that i have:
objCmd = new SqlCommand("SELECT * FROM tbl_accountants "+
"WHERE company LIKE @company" +
" OR country = @country", objConn);
objCmd.Parameters.Add("@company", "%" + txtCompany.Text + "%");
objCmd.Parameters.Add("@country", ddlCountry.SelectedItem.Value);
This does work, but it dosn't allow for if the user selects 'Any'. I imagine that the best way to acheive this is to add a if statement. Here is the code i have but it is not working:
objCmd = new SqlCommand("SELECT * FROM tbl_accountants "+
"WHERE company LIKE @company" +
if(@country == "Any"){ + " OR country = @country" + }, objConn);
objCmd.Parameters.Add("@company", "%" + txtCompany.Text + "%");
objCmd.Parameters.Add("@country", ddlCountry.SelectedItem.Value);
Thanks,
Jake

