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());
}
venkateshyel...
Member
12 Points
29 Posts
Populate a dropdown list from a list of table
Apr 13, 2012 04:26 PM|LINK
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());
}
bbcompent1
All-Star
32978 Points
8502 Posts
Moderator
Re: Populate a dropdown list from a list of table
Apr 13, 2012 04:28 PM|LINK
Could you post the error? Also, try running that query in the MySQL Workbench or command prompt to be sure it works.
basheerkal
Star
10672 Points
2426 Posts
Re: Populate a dropdown list from a list of table
Apr 13, 2012 04:33 PM|LINK
while(dr.Read()) { DropDownList1.Items.Add(dr.GetString(0).Trim()); }(Talk less..Work more)
ruipedromach...
Member
258 Points
84 Posts
Re: Populate a dropdown list from a list of table
Apr 13, 2012 10:50 PM|LINK
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();
bbcompent1
All-Star
32978 Points
8502 Posts
Moderator
Re: Populate a dropdown list from a list of table
Apr 16, 2012 01:23 PM|LINK
Yeah, you should rewrite your query like this: