CheckBoxList 绑定错误 (System.Data.Common.DataRecordInternal)

发布于 2024-10-06 16:54:48 字数 1367 浏览 3 评论 0原文

我希望我的问题能够描述自己=)我尝试了一切,但我似乎不明白为什么它不起作用!

我的 CheckBoxList :

    <asp:CheckBoxList ID="chkbxlstCuisines" runat="server">
    </asp:CheckBoxList>

我的代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        string cuisinesSelectStatement = "SELECT Cuisines.CuisineId, Cuisines.CuisineType FROM Cuisines";


        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString);
        SqlCommand comm = new SqlCommand(cuisinesSelectStatement , conn);
        SqlDataReader reader1;

        conn.Open();
        reader1 = comm.ExecuteReader();
        chkbxlstCuisines.DataSource = reader1;
        chkbxlstCuisines.DataBind();

        while (reader1.Read())
        {
            chkbxlstCuisines.DataValueField = reader1["CuisineId"].ToString();
            chkbxlstCuisines.DataTextField = reader1["CuisineType"].ToString();
        }    
            //conn.Close();
    }

我希望有人能弄清楚,我知道这会是一个小错误,因为我之前修复了这个错误,但现在我真的不知道出了什么问题! 预先感谢各位! =)

编辑:

我认为问题出在转换上,因为输出是五个复选框,而我的数据库恰好包含五个项目!

输出 :

System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal

I hope my question is describing itself =) I tried to do everything but I don't seem to get why it's not working!

My CheckBoxList :

    <asp:CheckBoxList ID="chkbxlstCuisines" runat="server">
    </asp:CheckBoxList>

My code-behind :

    protected void Page_Load(object sender, EventArgs e)
    {
        string cuisinesSelectStatement = "SELECT Cuisines.CuisineId, Cuisines.CuisineType FROM Cuisines";


        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString);
        SqlCommand comm = new SqlCommand(cuisinesSelectStatement , conn);
        SqlDataReader reader1;

        conn.Open();
        reader1 = comm.ExecuteReader();
        chkbxlstCuisines.DataSource = reader1;
        chkbxlstCuisines.DataBind();

        while (reader1.Read())
        {
            chkbxlstCuisines.DataValueField = reader1["CuisineId"].ToString();
            chkbxlstCuisines.DataTextField = reader1["CuisineType"].ToString();
        }    
            //conn.Close();
    }

I hope someone figure it out, I know it's gonna be a little mistake because I fixed this error before but now I really don't know what's wrong!
Thanks in advance guys! =)

Edit:

I think the problem is with the casting, because the output is five checkboxes and my database contains exactly five items!

OUTPUT :

System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal
System.Data.Common.DataRecordInternal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-10-13 16:54:48

你不需要“while”代码;您的代码应该是:

protected void Page_Load(object sender, EventArgs e)
{
    string cuisinesSelectStatement = "SELECT Cuisines.CuisineId, Cuisines.CuisineType FROM Cuisines";


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString);
    SqlCommand comm = new SqlCommand(cuisinesSelectStatement , conn);
    SqlDataReader reader1;
    chkbxlstCuisines.DataValueField = "CuisineId";
    chkbxlstCuisines.DataTextField = "CuisineType";
    conn.Open();
    reader1 = comm.ExecuteReader();
    chkbxlstCuisines.DataSource = reader1;

    chkbxlstCuisines.DataBind();


        //conn.Close();
}

DataValueField 是绑定对象中包含值的字段的名称。
DataTextField 是包含显示文本的绑定对象中字段的名称。

You don't need the "while" code; your code should be:

protected void Page_Load(object sender, EventArgs e)
{
    string cuisinesSelectStatement = "SELECT Cuisines.CuisineId, Cuisines.CuisineType FROM Cuisines";


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConnection"].ConnectionString);
    SqlCommand comm = new SqlCommand(cuisinesSelectStatement , conn);
    SqlDataReader reader1;
    chkbxlstCuisines.DataValueField = "CuisineId";
    chkbxlstCuisines.DataTextField = "CuisineType";
    conn.Open();
    reader1 = comm.ExecuteReader();
    chkbxlstCuisines.DataSource = reader1;

    chkbxlstCuisines.DataBind();


        //conn.Close();
}

DataValueField is the name of the field in your bound object that contains value.
DataTextField is teh name of teh field in your bound object that contains display text.

夏日落 2024-10-13 16:54:48
                cmd2.CommandText = "select * from upload";
                cmd2.Connection = con1;
                con1.Open();
                GridView1.DataSource = cmd2.ExecuteReader();
                GridView1.DataBind();
                con1.Close();
                cmd2.CommandText = "select * from document1";
                cmd2.Connection = con1;
                con1.Open();
                GridView2.DataSource = cmd2.ExecuteReader();
                GridView2.DataBind();
                con1.Close();
                cmd2.CommandText = "select * from upload";
                cmd2.Connection = con1;
                con1.Open();
                GridView1.DataSource = cmd2.ExecuteReader();
                GridView1.DataBind();
                con1.Close();
                cmd2.CommandText = "select * from document1";
                cmd2.Connection = con1;
                con1.Open();
                GridView2.DataSource = cmd2.ExecuteReader();
                GridView2.DataBind();
                con1.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文