i work with C#.net and mysql.

i will fill a combo box with data from stored procedure.
but i get only 1 value.

how can i fill the combobox?

Dani AI

Generated

is on the right track and ’s two options (bind a data source or add items manually) are the usual solutions. The symptom of “only one value” most often comes from how the reader/loop or binding is done, or from the stored procedure returning a single row. Practical guidance and a short example follow.

For data-binding: create a MySqlCommand whose CommandType is StoredProcedure and set its CommandText to the procedure name. Use a MySqlDataAdapter with that command to Fill a DataTable. After the table is filled, bind the table to the ComboBox and set the DisplayMember and ValueMember to the exact column names the procedure returns. This keeps UI code simple and preserves value/display pairs.

If preferring the manual Items approach (or for quick debugging), use a reader and add every row inside a loop. Example pattern:

using (var conn = new MySqlConnection(connString))
using (var cmd = new MySqlCommand("sp_GetItems", conn))
{
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    conn.Open();
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            comboBox1.Items.Add(rdr.GetString(0)); // or rdr["ColumnName"].ToString()
        }
    }
}

Checklist of common pitfalls:

  • Verify the stored procedure actually returns multiple rows by running it in a MySQL client.
  • When using a reader, use while(reader.Read()) — using if(reader.Read()) will only read the first row.
  • Ensure DisplayMember/ValueMember names match returned column names exactly.
  • Use using blocks (or explicitly Close/Dispose) so connections/readers aren’t left open.
  • If loading data on a background thread, marshal updates back to the UI thread (Invoke/BeginInvoke) before touching the ComboBox.

These points expand on ’s advice and target the typical mistakes seen when a ComboBox shows only one item.

Recommended Answers

All 4 Replies

>how can i fill the combobox?

Two ways:

1. DataBinding

2. Use Items collection to add list items.

i don't found something about databinding or items collection for stored procedures with c#.

Steps:

1. Populate the datatable instance using DataAdapter's Fill method.
2. Set DataSource, DisplayMember, and ValueMember properties.

comboBox1.DataSource=dataTab_instance;
 comboBox1.DisplayMember="column_name1";
 comboBox1.ValueMember="column_name1";

Steps:

1. Populate the datatable instance using DataAdapter's Fill method.
2. Set DataSource, DisplayMember, and ValueMember properties.

comboBox1.DataSource=dataTab_instance;
 comboBox1.DisplayMember="column_name1";
 comboBox1.ValueMember="column_name1";

for stored procedures i don't have DataAdpter.
i have:

MySqlConnection connection = new MySqlConnection(@"User ID=root;Password=oursupport;Host=192.168.1.25;Port=3306;Database=test; Direct=true;Protocol=TCP;Compress=false;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;");
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                connection.CreateCommand();

                command.CommandType = System.Data.CommandType.StoredProcedure;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.