Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!,

In C# winform application I want to display all the names of the tables which exists in my StoreDatabase. into a combobox (Dropdown).

In my Store database there are some tables which I want to show inside the dropdown. As the end-user clicks the dropdown, It will show all the lists of tables.

Here is the code Which I m trying 


What I have tried:

// C# Source Code :

DataTable dt = new DataTable();
MySqlConnection conn = new MySqlConnection(@"Server=localhost;Database=Generalstore;Username=my********;Password:********;");
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(@"show tables", conn);

da.Fill(dt);

            if (dt.Rows.Count > 0)
{
comboBox1.DisplayMember = "alloyName";
comboBox1.ValueMember = "alloyName";
comboBox1.DataSource = dt;
}
conn.Close();
}
Posted
Updated 13-Nov-22 16:05pm
Comments
[no name] 13-Nov-22 16:06pm    
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.objectcollection.add?view=windowsdesktop-6.0
M Imran Ansari 13-Nov-22 16:15pm    
Your code is looking good. What error you are getting?
adriancs 13-Nov-22 22:03pm    
@M Imran Ansari, he is typing the wrong column name. "alloyName" is not existed in "show tables"

1 solution

You are doing this:
C#
comboBox1.DisplayMember = "alloyName";
comboBox1.ValueMember = "alloyName";
comboBox1.DataSource = dt;
which "alloyName" is not existed.

You can type this in stead:
C#
comboBox1.Items.Clear();
foreach (DataRow dr in dt.Rows)
{
    comboBox1.Items.Add(dr[0] + "");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900