如何从数据库填充下拉列表?

时间:2021-04-19 14:15:05

So here I'm trying to populate my dropdown list, the code behind is as below:

所以在这里我试图填充我的下拉列表,背后的代码如下:

 protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
        con.Open();
        SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
        SqlDataReader ddlvalues = mycommand.ExecuteReader();
        ddlTransactionCategory.DataSource = ddlvalues;
        ddlTransactionCategory.DataTextField = "categoryCode";
        ddlTransactionCategory.DataValueField = "OrgID";
        ddlTransactionCategory.DataBind();


        mycommand.Connection.Close();
        mycommand.Connection.Dispose();
    }

the problem is, I can't seem to get it to work, any help? and is this code doing it right?

问题是,我似乎无法让它工作,任何帮助?这段代码做得对吗?

3 个解决方案

#1


1  

plz try below code:

请尝试以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
    con.Open();
    SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
    SqlDataAdapter adp =new   SqlDataAdapter(mycommand);
    DataSet ds =new DataSet();
    adp.Fill(ds);
    ddlTransactionCategory.DataSource = ds;
    ddlTransactionCategory.DataTextField = "categoryCode";
    ddlTransactionCategory.DataValueField = "OrgID";
    ddlTransactionCategory.DataBind();


    mycommand.Connection.Close();
    mycommand.Connection.Dispose();
}

Thanks, Hitesh

谢谢,Hitesh

#2


0  

Can't bind to a SqlDataReader (or at least I've never tried it). Get a DataTable or a DataSet instead, fill it and then bind that to the dropdown the same way.

无法绑定到SqlDataReader(或者至少我从未尝试过)。获取DataTable或DataSet,填充它然后以相同的方式将其绑定到下拉列表。

#3


0  

Use SqlDataAdataper like the one below

使用SqlDataAdataper,如下所示

using (SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
{
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from MSUnit", con);
        DataTable dt = new DataTable
        da.Fill(dt)
        ddlTransactionCategory.DataSource = dt;
        ddlTransactionCategory.DataTextField = "categoryCode";
        ddlTransactionCategory.DataValueField = "OrgID";
        ddlTransactionCategory.DataBind();
}

if you want to use DataReader you must insert one-by-one.

如果要使用DataReader,则必须逐个插入。

while (ddlvalues.Read())
{
    ddlTransactionCategory.Items.Add(new ListItem(ddlvalues.getString("OrgID"),ddlvalues.getString("categoryCode"))) 
}

#1


1  

plz try below code:

请尝试以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
    con.Open();
    SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
    SqlDataAdapter adp =new   SqlDataAdapter(mycommand);
    DataSet ds =new DataSet();
    adp.Fill(ds);
    ddlTransactionCategory.DataSource = ds;
    ddlTransactionCategory.DataTextField = "categoryCode";
    ddlTransactionCategory.DataValueField = "OrgID";
    ddlTransactionCategory.DataBind();


    mycommand.Connection.Close();
    mycommand.Connection.Dispose();
}

Thanks, Hitesh

谢谢,Hitesh

#2


0  

Can't bind to a SqlDataReader (or at least I've never tried it). Get a DataTable or a DataSet instead, fill it and then bind that to the dropdown the same way.

无法绑定到SqlDataReader(或者至少我从未尝试过)。获取DataTable或DataSet,填充它然后以相同的方式将其绑定到下拉列表。

#3


0  

Use SqlDataAdataper like the one below

使用SqlDataAdataper,如下所示

using (SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
{
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from MSUnit", con);
        DataTable dt = new DataTable
        da.Fill(dt)
        ddlTransactionCategory.DataSource = dt;
        ddlTransactionCategory.DataTextField = "categoryCode";
        ddlTransactionCategory.DataValueField = "OrgID";
        ddlTransactionCategory.DataBind();
}

if you want to use DataReader you must insert one-by-one.

如果要使用DataReader,则必须逐个插入。

while (ddlvalues.Read())
{
    ddlTransactionCategory.Items.Add(new ListItem(ddlvalues.getString("OrgID"),ddlvalues.getString("categoryCode"))) 
}