Populate a dropdown list from a list of tablehttp://forums.asp.net/t/1792664.aspx/1?Populate+a+dropdown+list+from+a+list+of+tableMon, 16 Apr 2012 13:23:44 -040017926644931533http://forums.asp.net/p/1792664/4931533.aspx/1?Populate+a+dropdown+list+from+a+list+of+tablePopulate a dropdown list from a list of table <p>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&nbsp;</p> <p>MySqlConnection connect = new MySqlConnection();<br> connect.ConnectionString = &quot;Data source=localhost;Database=locations;user id=root;password=ietmdb;&quot;;</p> <p>connect.Open();</p> <p>MySqlCommand cmd = new MySqlCommand(&quot;SELECT [Name] FROM TableName, ID TableID FROM locations WHERE xtype = 'u'&quot;, connect);<br> MySqlDataReader dr = cmd.ExecuteReader();</p> <p>while (dr.Read())<br> {<br> DropDownList1.Items.Add(dr.ToString());<br> }</p> 2012-04-13T16:26:03-04:004931538http://forums.asp.net/p/1792664/4931538.aspx/1?Re+Populate+a+dropdown+list+from+a+list+of+tableRe: Populate a dropdown list from a list of table <p>Could you post the error? Also, try running that query in the MySQL Workbench or command prompt to be sure it works.</p> 2012-04-13T16:28:24-04:004931548http://forums.asp.net/p/1792664/4931548.aspx/1?Re+Populate+a+dropdown+list+from+a+list+of+tableRe: Populate a dropdown list from a list of table <p></p> <blockquote><span class="icon-blockquote"></span> <h4>venkateshyeluri</h4> DropDownList1.Items.<span style="text-decoration:line-through">Add(dr.ToString</span>());<br> </blockquote> <p></p> <pre class="prettyprint">while(dr.Read()) { DropDownList1.Items.Add(dr.GetString(0).Trim()); }</pre> <p></p> 2012-04-13T16:33:32-04:004931872http://forums.asp.net/p/1792664/4931872.aspx/1?Re+Populate+a+dropdown+list+from+a+list+of+tableRe: Populate a dropdown list from a list of table <p>hi</p> <p>i think your problem might be with your SQL statement. i see that you are using &quot;FROM&quot; 2 times.</p> <p>&quot;&quot;SELECT [Name] FROM TableName, ID TableID FROM locations WHERE xtype = 'u'&quot;&quot;&quot;</p> <p>i dont think this syntax is incorrect.<br> <br> i would also sugest to put the result into a datatable a bind it to the dropdownbox.<br> <br> something like :<br> <br> mysqldataadapter a = new mysqldataadapter(&quot;SELECT.....&quot;,connect);</p> <p>datatable b = new datatable();</p> <p>a.fill(b);</p> <p>dropdownlist.datasource =&nbsp; b;</p> <p>dropdownlist.databind();</p> 2012-04-13T22:50:42-04:004934824http://forums.asp.net/p/1792664/4934824.aspx/1?Re+Populate+a+dropdown+list+from+a+list+of+tableRe: Populate a dropdown list from a list of table <p>Yeah, you should rewrite your query like this:</p> <pre class="prettyprint">&quot;SELECT [TableName].[Name], [TableID].[Locations] from TableName, TableID where [TableName].[xtype] = 'u'&quot;</pre> <p></p> 2012-04-16T13:23:44-04:00