1

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();

2 Answers 2

3

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);
}
....
Sign up to request clarification or add additional context in comments.

Comments

2

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.