user222427
user222427

Reputation:

C# MySql query result to combobox

I'm trying to query an MySql table, put all results into a combobox.

So my query results in apple 2220
I want to populate the combobox with apple2220
i'm having trouble getting the string outside of datarow.

        string MyConString = "SERVER=localhost;" +
                "DATABASE=iie;" +
                "UID=root;" +
                "PASSWORD=xxxx;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        string command = "select fruit,number from clientinformation";
        MySqlDataAdapter da = new MySqlDataAdapter(command,connection);
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach (DataRow row in dt.Rows)
        {
            string rowz = row.ItemArray.ToString();
        }
        connection.Close();

Upvotes: 1

Views: 12802

Answers (2)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

Try something along these lines:

...
foreach (DataRow row in dt.Rows)
{
   string rowz = string.Format("{0}:{1}", row.ItemArray[0], row.ItemArray[1]);
   yourCombobox.Items.Add(rowz);
}
....

Upvotes: 3

Bala R
Bala R

Reputation: 108937

Instead of

        foreach (DataRow row in dt.Rows)
        {
            string rowz = row.ItemArray.ToString();
        }

try this

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Fruit"; 
comboBox1.ValueMember = "Number";
comboBox1.DataBind();

Upvotes: 2

Related Questions