简体   繁体   中英

Binding Data from the Database to a Dropdown List in .NET

I am following a 3 Tier Architecture so I don't want to attach the data source in the aspx page like this:

<asp:DropDownList ToolTip="Select User" ID="users" runat="server" 
     DataSourceID="SqlDataSource1" DataTextField="CURRENCY_SHORT_DESCRIPTION" 
     DataValueField="CURRENCY_SHORT_DESCRIPTION" Width="100px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
     SelectCommand="SELECT DISTINCT &quot;Usernames&quot; FROM &quot;Users&quot;">
</asp:SqlDataSource>

I want to have a method in the datalayer which calls the same SQL Statement and receives back data, then I would have a method in the business layer which binds the data to the drop down list like this one:

private void populateUnitList()
{
    unitList.DataSource = DatabaseHandler.getUnitList();
    unitList.DataBind();
}

This is the code I have in the Database Handler:

public static string getUnitList()
{
    DatabaseAdapter dba = DatabaseAdapter.getInstance();
    string sqlQuery = ("SELECT DISTINCT Usernames FROM Users");
    return dba.queryDatabase(sqlQuery).ToString();
}

I am using C#, and .NET Framework 4.0. This is not working for me yet.


Update #3:

It is working as I want it too, here it goes:

//controller class
private void populateCurrencyList()
{
    currencyList.DataSource = DatabaseHandler.getCurrencyList();
    currencyList.DataTextField = "CURRENCY_SHORT_DESCRIPTION";
    currencyList.DataValueField = "CURRENCY_SHORT_DESCRIPTION";
    currencyList.DataBind();
}

//db handler class
public static DataTable getCurrencyList()
{
    DatabaseAdapter dba = DatabaseAdapter.getInstance();
    string sqlQuery = ("SELECT DISTINCT CURRENCY_SHORT_DESCRIPTION FROM CURRENCY");

    DataTable dt = new DataTable();
    dt.Load(dba.queryDatabase(sqlQuery));
    return dt;
}

//controller class, the !IsPostBack prevents duplication of fields
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        populateUnitList();
        populateCurrencyList();
    }
}
SqlConnection  conn = new SqlConnection("Connectionstring")
SqlCommand cmd = new SqlCommand ();

cmd.CommandText = " Select * From GetUsersID";
cmd.Connection = conn;
conn.Open();
DataTable dt  = new DataTable();

dt.Load(cmd.ExecuteReader());
conn.Close();

DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind(); `

You have to mention the DataTextField and DataValueField property of the drop down list. DataTextField is the field to display and DataValueField is the value of that field

A DataSource must implement IEnumerable but your database handler methods returns a string. I don't know what your databaseAdapter class is doing but it should expose an enumerable collection of rows from the database and this is what you need to return.

<asp:DropDownList ID="ddl1" DataTextField="Desc" DataValueField="Id"
runat="server" />


List<yourclass> operation = new List<yourclass>();
ddl1.DataSource = GetOperationData; //get connect to your BAL or DAL
ddl1.DataTextField = "Desc";
ddl1.DataValueField = "Id";
ddl1.DataBind();

You need to use an Objectdatasource in your aspx page rather than a Sqldatasource

see: http://msdn.microsoft.com/en-us/library/9a4kyhcx.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM