locked
Populate a dropdown list from a list of table RRS feed

  • Question

  • User-677160207 posted

    I trying to get tables names from my MySQL database to dropdown list box. I have written code for this, it left me some errors. Kindly help 

    MySqlConnection connect = new MySqlConnection();
    connect.ConnectionString = "Data source=localhost;Database=locations;user id=root;password=ietmdb;";

    connect.Open();

    MySqlCommand cmd = new MySqlCommand("SELECT [Name] FROM TableName, ID TableID FROM locations WHERE xtype = 'u'", connect);
    MySqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
    DropDownList1.Items.Add(dr.ToString());
    }

    Friday, April 13, 2012 12:26 PM

Answers

  • User-718146471 posted

    Yeah, you should rewrite your query like this:

    "SELECT [TableName].[Name], [TableID].[Locations] from TableName, TableID where [TableName].[xtype] = 'u'"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2012 9:23 AM

All replies

  • User-718146471 posted

    Could you post the error? Also, try running that query in the MySQL Workbench or command prompt to be sure it works.

    Friday, April 13, 2012 12:28 PM
  • User-232462845 posted

    DropDownList1.Items.Add(dr.ToString());

    while(dr.Read())
                {
                    DropDownList1.Items.Add(dr.GetString(0).Trim());
                }

    Friday, April 13, 2012 12:33 PM
  • User1013750657 posted

    hi

    i think your problem might be with your SQL statement. i see that you are using "FROM" 2 times.

    ""SELECT [Name] FROM TableName, ID TableID FROM locations WHERE xtype = 'u'"""

    i dont think this syntax is incorrect.

    i would also sugest to put the result into a datatable a bind it to the dropdownbox.

    something like :

    mysqldataadapter a = new mysqldataadapter("SELECT.....",connect);

    datatable b = new datatable();

    a.fill(b);

    dropdownlist.datasource =  b;

    dropdownlist.databind();

    Friday, April 13, 2012 6:50 PM
  • User-718146471 posted

    Yeah, you should rewrite your query like this:

    "SELECT [TableName].[Name], [TableID].[Locations] from TableName, TableID where [TableName].[xtype] = 'u'"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 16, 2012 9:23 AM