将下拉列表绑定到数据库

时间:2010-11-22 06:10:27

标签: c# asp.net drop-down-menu

我想将下拉列表绑定到数据库。我做了以下编码,但它无法正常工作。

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 

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

4 个答案:

答案 0 :(得分: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);
        }
    }

答案 1 :(得分: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();
    }

答案 2 :(得分: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);

    }   

自从我用ado做了这个金属以来,已经很久了。但是它应该更像读者部分。

然后是绑定。

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

答案 3 :(得分:0)

您的查询正在查询列casename,casecode

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

编辑 -

但是,在绑定时,您将不同的列绑定到datatextfield和datavaluefields。

您正在使用sql字符串变量作为数据源。您应该使用datareader。

尝试使用 -

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