本文介绍了将数据从数据库填充到c#checkboxlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述我的mssql数据库中有一个表有2列(homecellid和homecellname)我希望homecell中的所有数据都显示在checkboxlist(cblMinistries)中我尝试了这个但我不断收到错误类型或命名空间名称 ListItem无法找到请帮帮我

private void PopulateMinistires() { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings [ConnectionString]); conn.ConnectionString =数据源= USER-PC;初始目录= PIWCDB;用户ID = sa;密码=迈克; 使用(SqlCommand cmd = new SqlCommand()) { cmd.CommandText =select * from tblMinistries; cmd.Connection = conn; conn.Open(); using(SqlDataReader sdr = cmd.ExecuteReader()) { while(sdr.Read()) { ListItem item = new ListItem(); item.Text = sdr [Homecellname]。ToString(); chkMinistries.Items.Add(item); } } conn.Close(); } } 

解决方案 

Richard C. Bishop的解决方案1可能对您有所帮助,但如果没有,请参考:无法找到类型或命名空间名称\’ListItem\’ [ ^ ]。为Web窗体添加此内容:

使用System.Web.UI.WebControls;for Win Forms:

使用System.Windows.Forms;到顶部您的页面或使用完全限定的名称对于控件。

 

嗨Mike,请看这些例子: 使用DataBase绑定CheckBoxList [ ^ ] 使用CheckBoxList填充 [ ^ ] 问候。 

i have a table in my mssql db with 2 columns (homecellid and homecellname) i want all data in homecell to be displayed in a checkboxlist (cblMinistries) i tried this but i keep getting an error \”The type or namespace name \”ListItem\” could not be found\” please help me out

private void PopulateMinistires() { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[\”ConnectionString\”]); conn.ConnectionString = \”Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike\”; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = \”select * from tblMinistries\”; cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { ListItem item = new ListItem(); item.Text = sdr[\”Homecellname\”].ToString(); chkMinistries.Items.Add(item); } } conn.Close(); } } 

解决方案 

Solution 1 by Richard C. Bishop might help you, but if not, please, refer this: The type or namespace name \’ListItem\’ could not be found[^]. 

Either add this for Web Forms: 

using System.Web.UI.WebControls;of this for Win Forms:

using System.Windows.Forms;to the top of your page or use the fully qualified name for the control.

 

Hi Mike, see these examples: Bind CheckBoxList with DataBase[^] Populate with CheckBoxList[^] Regards. 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注