将数据从数据库填充到c#checkboxlist [英] populating data from database onto c# checkboxlist

查看:64
本文介绍了将数据从数据库填充到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' [< a href =http://social.msdn.microsoft.com/Forums/en-US/2394efee-7fba-493c-8bf5-fcac34366cca/the-type-or-namespace-name-listitem-could-not-be-发现你正在丢失一个使用指令或论坛?论坛= csharpgeneraltarget =_ blanktitle =新窗口> ^ ]。


< blockquote>为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.


这篇关于将数据从数据库填充到c#checkboxlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆