【问题标题】:Checkboxlist multiple value select and display in gridview in asp.net复选框列表多值选择并在asp.net的gridview中显示
【发布时间】:2014-05-27 01:55:46
【问题描述】:

我将 C# 用于 Web 应用程序将复选框列表。我在下面写我的代码。当我从复选框列表中选择多个值时,它会报告错误。但是只有一个值选择,它工作正常。

代码分为两部分:
1. select from checkboxlist (multichoice):与数据库中的值相同,存储相关数据。
2.向gridview显示值:选择后,将整个表格显示到gridview显示。

我的错误是从复选框中选择 2 个或更多值时,错误提示“变量名称 @textInput 已被声明。变量名称在查询批处理或存储过程中必须是唯一的。”

谁能帮我解决这个问题?或者你有任何其他方法可以做到这一点。

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TestGridview : System.Web.UI.Page
{
private int count = 0;
private DataSet dc = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)
{

    // Create the list to store.
    List<String> YrStrList = new List<string>();
    foreach (ListItem item in CheckBoxList1.Items)
    {
        if (item.Selected)
        {
            YrStrList.Add(item.Value);
        }
    }

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["XMLConnectionString"].ConnectionString);
    // Create the command object
    string str = "SELECT * FROM XML WHERE [Part_Numbber] = @textInput";
    SqlCommand cmd = new SqlCommand(str, con);

    for (int d = 0; d < YrStrList.Count; d++)
    {
        DataSet ds = new DataSet();
        string text;
        text = YrStrList[d];
        cmd.Parameters.AddWithValue("textInput", text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds, "XML");
        if (count == 0) {

            dc = ds.Clone();
            count++;
        }


        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i].ItemArray[0].ToString() != "NULL")
                dc.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
        }
    }


    GridView1.DataSource = dc;
    GridView1.DataBind();
}

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
}

}

【问题讨论】:

  • 请附上您遇到的错误。为了便于选择使用 Linq: IEnumerable CheckedItems = CheckBoxList1.Items.Cast() .Where(i => i.Selected) .Select(i => i.Value);不要忘记添加 System.Linq 命名空间。
  • 我永远不会在循环中调用 SQL,而是为您获取 SQL View/Table 工作并使用 Linq 进行过滤。
  • 对不起你的意思@codebased
  • 你的意思是我可以使用“Linq”而不是“SQL”吗?@codebased
  • 你必须使用 sql 来获取数据,但之后如果你需要某种过滤,根据条件选择数据,你可以使用 linq 来做到这一点,而不是通过行循环

标签: c# asp.net sql gridview checkboxlist


【解决方案1】:

由于您重复执行相同的查询,您可以在循环外部添加参数,然后将它们填充到内部。

SqlCommand cmd = new SqlCommand(str, con);
command.Parameters.Add(new SqlParameter("@textInput", 0));

 for (int d = 0; d < YrStrList.Count; d++)
    {
 string text;
        text = YrStrList[d];
command.Parameters["@textInput"].Value = text ;
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多