0

I have 2 drop-down boxes and i want the first drop-down to filter the 2nd one like cascading so the first one has check-boxes and i want to pass the selected items from the first one to the 2nd but getting a "syntax error". I run it into debug mode and the output result looks like this

CommandText "select  id, name FROM myTable where id in (''CKU019','CW5036'') "

and it seems there is extra apostrophe near CKU019. Here is my code

protected void bindDDL()
    {
      string selectedValues = string.Empty;
      foreach (ListItem item in ddchkCountry.Items)
      {
        if (item.Selected)
          selectedValues += "'" + item.Value + "',";
      }
      if (selectedValues != string.Empty)
        selectedValues = selectedValues.Remove(selectedValues.Length - 1);
      SqlConnection con = new SqlConnection(strConnString);
      con.Open();
      SqlCommand cmd = new SqlCommand("select  id, name FROM myTable where id in ('" + selectedValues + "')", con);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      con.Close();
      facDDL.DataSource = ds;
      facDDL.DataTextField = "name";
      facDDL.DataValueField = "id";
      facDDL.DataBind();
    }

1 Answer 1

1

Remove the extra ' wrapping your selected values...

SqlCommand cmd = new SqlCommand("select id, name FROM myTable where id in (" + selectedValues + ")", con);

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, please remember to mark this as the answer if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.