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?
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:
These points expand on ’s advice and target the typical mistakes seen when a ComboBox shows only one item.
Jump to Post— kvprajapati 1,826>how can i fill the combobox?
Two ways:
1. DataBinding
2. Use Items collection to add list items.
>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; We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.