0

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.

2
  • Do you have a dataset of some sort already, or are you looking for a complete example? Commented Nov 5, 2012 at 5:42
  • please post related code in your .aspx.cs for us to check the problem Commented Nov 5, 2012 at 6:17

3 Answers 3

2

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();

     }
Sign up to request clarification or add additional context in comments.

1 Comment

How do you implement the OdbcConnection statement as well as the Obdc Command...? I type it in but it doesn't even pick up this command.
1

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

Drop down list asp.net

2 Comments

This was useful however I cannot implement some of the commands for MySQL here...
hi if you want to use sql command in visual studio you should include these libraries. Using System.Data and Using System.Data.SqlClient.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.