当前位置: 代码迷 >> ASP.NET >> 下拉菜单绑定数据库有关问题 asp.net(c#)
  详细解决方案

下拉菜单绑定数据库有关问题 asp.net(c#)

热度:5167   发布时间:2013-02-25 00:00:00.0
下拉菜单绑定数据库问题 asp.net(c#)
我的目的是下拉菜单绑定数据库,当初始化时把下拉菜单的内容是从数据库里读的,是Dname字段
可是我编了如下代码,但下拉菜单显示的是System.Data不知道是什么原因啊,我刚接触asp.net,请高手指导,写的详细点,连接数据库肯定是连上了,我用的是vs2008
[code=C#][/code]

  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = "";
  conn.Open();
  string strSel = "select Dname from Drug";
  SqlCommand selcom = new SqlCommand(strSel, conn);
  SqlDataAdapter da = new SqlDataAdapter();
  da.SelectCommand = selcom;
  DataSet ds = new DataSet();
  da.Fill(ds, "account");

  this.DropDownList1.DataSource = ds.Tables["account"].DefaultView;
  this.DropDownList1.DataBind();

------解决方案--------------------------------------------------------
你的绑定应该没问题,只是没加显示的字段,呵呵。
------解决方案--------------------------------------------------------
探讨
C# code
this.DropDownList1.DataSource = ds.Tables["account"].DefaultView;
this.DropDownList1.DataTextField="要显示哪个字段";
this.DropDownList1.DataValueField="值字段";
this.DropDownList1.DataBind();