CheckBoxList 绑定错误 (System.Data.Common.DataRecordInternal)
我希望我的问题能够描述自己=)我尝试了一切,但我似乎不明白为什么它不起作用!
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要“while”代码;您的代码应该是:
DataValueField 是绑定对象中包含值的字段的名称。
DataTextField 是包含显示文本的绑定对象中字段的名称。
You don't need the "while" code; your code should be:
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.