2010-11-22 111 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(); 
    }