2010-11-22 112 views
0

我想将下拉列表绑定到数据库。我做了以下编码,但它不起作用。将下拉列表绑定到数据库

using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.Odbc; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

      rebind(); 

     } 
    } 

    public void rebind() 
    { 

     try 
     { 
     OdbcConnection myConn = new OdbcConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);  
     string sql = "select casename,casecode from casetype"; 
     myConn.Open(); 
     OdbcCommand cmd = new OdbcCommand(sql, myConn); 
     OdbcDataReader MyReader = cmd.ExecuteReader(); 
        { 
         DropDownList3.DataSource= sql; 
         DropDownList3.DataTextField = "casename"; 
         DropDownList3.DataValueField = "casetype"; 
         DropDownList3.DataBind(); 
        } 
       } 

     catch (Exception ex) 
     { 

      Response.Write(ex.StackTrace); 

     } 
    } 
} 

我收到错误的

at _Default.rebind() in c:\Documents and Settings\a\My Documents\Visual Studio 2008\WebSites\toolbar1\Default.aspx.cs:line 32 

请帮我解决我的问题,并绑定下拉列表的datasource.I想我的下拉列表中显示从数据库列文本,并使用值字段在稍后的代码中用于其他目的。我在运行项目时显示页面,但无法获取下拉列表中的数据

回答

2

你有没有尝试使用DataAdapter的这样吗?

public void rebind() 
    { 

     try 
     { 
      OdbcConnection myConn = new OdbcConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString); 
      string sql = "select casename,casecode from casetype"; 
      myConn.Open(); 
      OdbcCommand cmd = new OdbcCommand(sql, myConn); 
      OdbcDataAdapter adapter = new OdbcDataAdapter(cmd); 
      DataTable dt = new DataTable(); 
      adapter.Fill(dt); 
      DropDownList3.DataSource = dt; 
      DropDownList3.DataTextField = "casename"; 
      DropDownList3.DataValueField = "casecode"; 
      DropDownList3.DataBind(); 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.StackTrace); 
     } 
    } 
0

您正在将sql的字符串设置为数据源,而不是数据结构。

将您的对象读出到列表或Ilist子类型中。

然后将其绑定到下拉菜单。

List<CaseType> ct = new List<CaseType>(); 
    try 
    { 
    OdbcConnection myConn = new OdbcConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);  
    string sql = "select casename,casecode from casetype"; 
    myConn.Open(); 
    OdbcCommand cmd = new OdbcCommand(sql, myConn); 
    OdbcDataReader MyReader = cmd.ExecuteReader(); 
    while(MyReader.ReadNext()){ 
        ct.Add(new CaseType(){Name = MyReader.Read("casename").ToString(), Type = Convert.ToInt32(MyReader.Read("casetype"))}); 
      } 
} 
    catch (Exception ex) 
    { 

     Response.Write(ex.StackTrace); 

    } 

它已经很长一段时间,因为我已经做了这个金属的东西很长。但是在读者部分应该更像这样。

然后绑定事后。

    DropDownList3.DataSource= ct; 
        DropDownList3.DataTextField = "Name"; 
        DropDownList3.DataValueField = "Type"; 
        DropDownList3.DataBind(); 
+0

如何的我使用代码它做什么,可以帮助我用一个例子或一个适当的方式来解决我的问题 – Ishan 2010-11-22 06:19:18

0

您的查询查询列casename,casecode

string sql = "select casename,casecode from casetype"; 

编辑 -

但是,虽然绑定你不同的列绑定到您的datatextfield和datavaluefields。

您正在使用sql字符串变量作为数据源。您应该使用您的数据读取器。

尝试使用 -

DropDownList3.DataSource= MyReader; 
DropDownList3.DataTextField = "casename"; 
DropDownList3.DataValueField = "casecode"; 
+0

@pavelred那个力量帮助我。 – Ishan 2010-11-22 06:19:57

+0

你想让你的下拉列表显示(文字和数值)?而且,错误是什么? – pavanred 2010-11-22 06:24:09

+0

我希望我的下拉列表显示来自数据库列的文本,并在代码中稍后将值字段用于其他用途。我得到的页面显示,当我运行该项目,但无法获得下拉列表中的数据 – Ishan 2010-11-22 06:33:14

1

下面的代码尝试....它一定会工作.... 在web.config中首先定义的ConnectionString内

protected void Page_Load(object sender, EventArgs e) 
    { 
     string strconnection = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString(); 
     SqlConnection con = new SqlConnection(strconnection); 
     con.Open(); 

     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "select ename,ecompany from example"; 

     SqlDataAdapter adp = new SqlDataAdapter(cmd); 

     DataSet ds = new DataSet(); 
     adp.Fill(ds, "example"); 

     DropDownList1.DataSource = ds; 
     DropDownList1.DataTextField = "ename"; 
     DropDownList1.DataValueField = "ecompany"; 
     DropDownList1.DataBind(); 
    }