我希望我的问题是描述自己=)我试图做所有事情,但我似乎不明白为什么它不起作用!
我的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
答案 0 :(得分:1)
您不需要“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是绑定对象中包含显示文本的teh字段的名称。
答案 1 :(得分:0)
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();