Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

SQL SELECT - filtering

Hi All, this should be an easy one.

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
[1435 byte] By [silencer01] at [2007-11-11 6:38:15]
# 1 Re: SQL SELECT - filtering
Since you're using SQL query strings why not just build them on the fly? If the user selects 'Any' from the drop down then omit the country criteria from the string and don't add the country parameter.
pclement at 2007-11-11 23:48:06 >
# 2 Re: SQL SELECT - filtering
Thanks for your response Paul,

I not sure exactly how to build the SQL statement on the fly. I have tried the following:

if(ddlCountry.SelectedItem.Value != "Any"){
objCmd.Parameters.Add("@country", ddlCountry.SelectedItem.Value);
}

But the SQL will still look for the @country variable.
silencer01 at 2007-11-11 23:49:14 >
# 3 Re: SQL SELECT - filtering
Are you still referencing the country parameter criteria in your SQL string?

Build your SQL string outside of the SQLCommand instantiation. Use the same logic you are using to determine whether to add the country parameter and append the country criteria to the SQL string if the user selects 'Any'.
pclement at 2007-11-11 23:50:12 >
# 4 Re: SQL SELECT - filtering
Here's an example of what Paul is suggesting:

SqlCommand objCmd = new SqlCommand();
string SQL = "SELECT * FROM tbl_accountants " +
"WHERE company LIKE @company";
if(@country != "Any")
{
SQL += " AND country = @country";
objCmd.Parameters.Add("@country", ddlCountry.SelectedItem.Value);
}
objCmd.Parameters.Add("@company", "%" + txtCompany.Text + "%");
objCmd.CommandText = SQL;
objCmd.Connection = objConn;
Phil Weber at 2007-11-11 23:51:12 >
# 5 Re: SQL SELECT - filtering
Thanks once again Phil, the code works great! There was one small change that i had to make for it to work for me, it was to do with the if statement. Here is my change:

if(ddlCountry.SelectedItem.Value != "Any")
{
SQL += " AND country = @country";
objCmd.Parameters.Add("@country", ddlCountry.SelectedItem.Value);
}

Thanks again for your help.

Jake
silencer01 at 2007-11-11 23:52:16 >