private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
string query = "SELECT * FROM Offers";
//Countries
string countries = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
countries += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(countries))
{
countries = string.Format(" where offer_type in ({0})", countries.Substring(0, countries.Length - 1));
}
//States
string states = string.Empty;
foreach (ListItem item in chkCountries.Items)
{
states += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(states))
{
if (!string.IsNullOrEmpty(countries))
{
states += " where";
}
else
{
states += " and";
}
states += string.Format(" where store_name in ({0})", states.Substring(0, states.Length - 1));
}
SqlCommand cmd = new SqlCommand(query + countries + states);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
that code show error when click on second checkbox list (//States). how can i resolve that issue.
Error Msg: System.Data.SqlClient.SqlException: Incorrect syntax near ' '.