TerNovi
TerNovi

Reputation: 400

Populate dropdown list using c# containing MySQL db table

I am trying to populate the following dropdown list with information from MySQL database.

<asp:DropDownList ID="DeleteUsersList" runat="server" AutoPostBack="True" 
    onselectedindexchanged="DeleteUsersList_SelectedIndexChanged"></asp:DropDownList>

I am using a .aspx source file for the html that contains the dropdown list. I also have .aspx.cs file that contains C# code and I'm usying MySQL server for the database.

Basically I am trying to populate the dropdown list using a c# connection to MySQL database when the page is loaded. I have not been able to find anything specific with this information so if anyone can help me it would greatly be appreciated.

Thanks ahead.

Upvotes: 0

Views: 4393

Answers (3)

user1711092
user1711092

Reputation:

You could bind the DropDownList to a data source (DataTable, List, DataSet, SqlDataSource, etc) that you get from MYSQL database.

For example, if you wanted to use a DataTable:

private string GetConnection()
 {
     return "DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;Database=testdatabase";
 }

    private void LoadUsers()
    {

     DataTable rt = new DataTable();
     DataSet ds = new DataSet();
     OdbcDataAdapter da = new OdbcDataAdapter();
     OdbcConnection con = new OdbcConnection(GetConnection());
     OdbcCommand cmd = new OdbcCommand(sql, con);
     da.SelectCommand = cmd;
     da.Fill(ds);         
     rt = ds.Tables[0];


      DeleteUsersList.DataSource = rt;
      DeleteUsersList.DataTextField = "UserName";
      DeleteUsersList.DataValueField = "UserID";
      DeleteUsersList.DataBind();

     }

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222700

Hi check the following link to read about drop down example in asp.net

Drop down list asp.net

Upvotes: 1

nilobarp
nilobarp

Reputation: 4094

Execute "show tables" against your MySQL connetion. This will give you the list of available tables.

Upvotes: 0

Related Questions