在Asp.net中使用Drop-down和Check-box

时间:2017-03-18 19:56:40

标签: c# asp.net .net

我有2个下拉框,我希望第一个下拉过滤第二个像级联,所以第一个有复选框,我想将选定的项目从第一个传递到第二个但是获得一个“语法错误”。我将它运行到调试模式,输出结果如下所示

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

似乎在CKU019附近有额外的撇号。这是我的代码

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 个答案:

答案 0 :(得分:1)

删除额外的'包装所选值...

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