标题:why dropdownlist don't bind
只看楼主
apple0129
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2008-3-28
 问题点数:0 回复次数:4 
why dropdownlist don't bind
private void GetSourceListData()
            string cmdText = "SECECT powerid,powername from power";
            DataTable table =ReturnSqlex(cmdText);
            
                SoureList.DataSource = table.DefaultView;
                SoureList.DataTextField = "powername";
                SoureList.DataValueField = "powerid";
                SoureList.DataBind();
                SoureList.Items.Insert(0, new ListItem("请选择", ""));
                SoureList.DataBind();

        }

        public DataTable ReturnSqlex(string strSql)//调用此方法返回一个datatable
        {

            SqlConnection con = new SqlConnection(
                ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"]);

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(strSql, con);
                SqlDataAdapter sdr = new SqlDataAdapter();
                sdr.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sdr.Fill(ds, "table");
                return ds.Tables["table"];
            }
            catch (SqlException ex)
            {
                //抛出异常
                throw new Exception(ex.Message, ex);
            }
            finally
            {   ///关闭链接
                con.Close();
            }
        }
搜索更多相关主题的帖子: why bind dropdownlist don 
2008-04-09 22:22
hebingbing
Rank: 6Rank: 6
来 自:黄土高坡
等 级:贵宾
威 望:27
帖 子:3417
专家分:371
注 册:2007-10-22
得分:0 
将代码改成这样的:
SoureList.DataSource = table.DefaultView;
                SoureList.DataTextField = "powername";
                SoureList.DataValueField = "powerid";
                SoureList.DataBind();
                SoureList.Items.Insert(0, new ListItem("请选择", ""));
             //   SoureList.DataBind();
插入是不需要再绑定的……
2008-04-09 22:32
apple0129
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2008-3-28
得分:0 
回复 2# 的帖子
已经去掉了,运行没错误,但就是没有显示数据,而数据库中确实有数据,怎么回事啊,已经好几天了,一直没有解决啊,
2008-04-09 22:40
hebingbing
Rank: 6Rank: 6
来 自:黄土高坡
等 级:贵宾
威 望:27
帖 子:3417
专家分:371
注 册:2007-10-22
得分:0 
绑定代码应该是没有问题的,你先把绑定的代码注释掉,然后:
DataRow[] rs=table.rows;
foreach(DataRow row in rs)
{
response.write(row["powername"]);
response.write(row["powerid"]);
}
要是没有数据则就是没有读出数据,要是有那就是绑定的问题……
要不光会写程序还要学会调试程序……
2008-04-09 22:52
apple0129
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2008-3-28
得分:0 
我已经把rows改成了Rows,response.write改成了Response.Write 但是又出现了错误,"proid"字段是int 型的
无法将类型“System.Data.DataRowCollection”隐式转换为“System.Data.DataRow[]”
2008-04-10 00:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208095-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017043 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved