Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法级联多个复选框项_C#_Asp.net_.net - Fatal编程技术网

C# 无法级联多个复选框项

C# 无法级联多个复选框项,c#,asp.net,.net,C#,Asp.net,.net,我正在使用下面的代码级联我的复选框列表项。当我选中一个复选框时,我的问题层叠工作,当我选中多个复选框时,层叠不工作。逗号分隔的值有问题吗 protected void cblGroup_SelectedIndexChanged(object sender, EventArgs e) { string selectedValues = string.Empty; foreach (ListItem item in cblGroup.Items)

我正在使用下面的代码级联我的复选框列表项。当我选中一个复选框时,我的问题层叠工作,当我选中多个复选框时,层叠不工作。逗号分隔的值有问题吗

protected void cblGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValues = string.Empty;
        foreach (ListItem item in cblGroup.Items)
        {
            if (item.Selected)
                selectedValues += item.Value + ",";
        }
        if (selectedValues != string.Empty)
            selectedValues = selectedValues.Remove(selectedValues.Length - 1);
        SqlConnection con = new SqlConnection(strcon);
        con.Open();      
        SqlCommand cmd = new SqlCommand("select [Code] from Details where [Group] in ('" + selectedValues + "')", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        cblCode.DataSource = ds;
        cblCode.DataTextField = "Code";
        cblCode.DataValueField = "Code";
        cblCode.DataBind();                   
    }

您是否不需要在关键字中使用SQL
,而不是
=
——因为您的查询使用的是逗号分隔的值

e、 g.
从[COL]位于('1','2','3')的表格中选择*

如果只选择了1项,则where子句中不会有任何逗号,因此查询工作正常-如果选择了更多项,则查询不会返回任何结果


当选择单个项目时,在
中使用
也会起作用。

您是否不需要在
关键字中使用SQL
,而不是
=
——因为您的查询使用的是逗号分隔的值

e、 g.
从[COL]位于('1','2','3')的表格中选择*

如果只选择了1项,则where子句中不会有任何逗号,因此查询工作正常-如果选择了更多项,则查询不会返回任何结果


当选择单个项目时,在
中使用
也会起作用。

您必须为所有选择的值添加引号。试试这个:

selectedValues+=“'”+项。值+“,”

然后像SpaceBison所说的那样使用

protected void cblGroup_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValues = string.Empty;
    foreach (ListItem item in cblGroup.Items)
    {
        if (item.Selected)
            selectedValues += "'" + item.Value + "',";
    }
    if (selectedValues != string.Empty)
        selectedValues = selectedValues.Remove(selectedValues.Length - 1);
    SqlConnection con = new SqlConnection(strcon);
    con.Open();      
    SqlCommand cmd = new SqlCommand("select [Code] from Details where [Group] in (" + selectedValues + ")", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    cblCode.DataSource = ds;
    cblCode.DataTextField = "Code";
    cblCode.DataValueField = "Code";
    cblCode.DataBind();                   
}

您必须为所有选定的值添加引号。试试这个:

selectedValues+=“'”+项。值+“,”

然后像SpaceBison所说的那样使用

protected void cblGroup_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedValues = string.Empty;
    foreach (ListItem item in cblGroup.Items)
    {
        if (item.Selected)
            selectedValues += "'" + item.Value + "',";
    }
    if (selectedValues != string.Empty)
        selectedValues = selectedValues.Remove(selectedValues.Length - 1);
    SqlConnection con = new SqlConnection(strcon);
    con.Open();      
    SqlCommand cmd = new SqlCommand("select [Code] from Details where [Group] in (" + selectedValues + ")", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    cblCode.DataSource = ds;
    cblCode.DataTextField = "Code";
    cblCode.DataValueField = "Code";
    cblCode.DataBind();                   
}


您能否创建一个字符串局部变量,它是构造的sql并在调试控制台中打印该值?然后,您可以直接测试SQL,并知道问题是在SQL中还是在代码中。是否可以创建一个字符串局部变量(即构造的SQL)并在调试控制台中打印该值?然后您可以直接测试SQL,并知道问题是在SQL中还是在代码中?SqlCommand cmd=new SqlCommand(“从(““+selectedValues+”)”中[Group]所在的详细信息中选择[Code]”,con);仍然是一样的:(如果我选择了多个复选框,则其各自的项目不会出现在第二个复选框中。选择一个复选框时效果良好。这是我选择多个复选框时形成查询的方式。因此,问题可能在于逗号分隔的值。从[Group]所在的详细信息中选择[Code]('Item1,Item2')@user3003821-您需要在每个项目周围加引号,例如,'1','2')
中的
,正如我的例子所示。'Item1,Item2'和您原来的SQL返回的内容没有区别。像这样?SqlCommand cmd=new SqlCommand(“从('+selectedValues+“')中[Group]所在的详细信息中选择[code],con);仍然是一样的:(如果我选择了多个复选框,则其各自的项目不会出现在第二个复选框中。选中一个复选框时效果良好。这是我选择多个复选框时形成查询的方式。因此,问题可能出在逗号分隔的值上。从[Group]所在的详细信息中选择[Code]('Item1,Item2')@user3003821-您需要在每个项目周围加引号,例如('1','2'中的
根据我的示例!就SQL将返回的内容而言,“Item1,Item2”与您最初拥有的内容没有区别。我对使用此框架查询感到有点困惑。您能详细说明一下吗?困惑的是什么?在中使用还是构造查询?构造查询。您正在像这样构造查询:in('Item1,Item2'))。但是您的所有项目都必须位于简单引号之间,如:in('Item1','Item2')。因此,在使用逗号添加项目时,只需添加所需的引号。我添加了这行代码selectedValues+=“'”+item.Value+”,“我的项目在中公开”而不是像“”我有点困惑使用这个框架查询。你能详细说明我吗?你困惑什么?使用还是构造查询?构造查询。你像这样构造你的查询:IN('Item1,Item2')。但是你的所有项目都必须在简单的引号之间,比如:'IN('Item1','Item2'))。因此,在为项目添加逗号时,只需添加所需的引号。我添加了这行代码selectedValues+=“'”+item.Value+“,“我的项目在“”中公开,与“”不同”