Stack Exchange
Populating ComboBox
My scenario is to populate combobox from the database, display customer name, and hold a value of the id using identity increment.
When this code is run, I receive an error Procedure or function 'spSelectCustomerById' expects parameter '@id', which was not supplied.
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

//SelectCustomerById(int x);
comboBoxEx1.Items.Clear();

SqlCommand comm = new SqlCommand("spSelectCustomerByID", conn);
//comm.Parameters.Add(new SqlParameter("cust_name", cust_name));
//comm.CommandText = "spSelectCustomerByID";
comm.Parameters.Add(new SqlParameter("cust_id", SqlDbType.Int));
comm.CommandType = CommandType.StoredProcedure;
comm.ExecuteNonQuery();

SqlDataAdapter sdap = new SqlDataAdapter(comm);
DataSet dset = new DataSet();
sdap.Fill(dset, "cust_registrations");

if (dset.Tables["cust_registrations"].Rows.Count > 0)
{
    comboBoxEx1.Items.Add("cust_registrations").ToString();
}
comboBoxEx1.DataSource = dset;
comboBoxEx1.DisplayMember = "cust_name";

How can I populate a combobox from a database?

So What is your question??

Why are you doing comm.ExecuteNonQuery(); and then filling the dataset using dataadapter, you don't need comm.ExecuteNonQuery();

I think this code has some problems: aside from heavy SRP violation: you add a sql-parameter (cust_id) without setting the value; you add items and at the same time add a datasource/display member (you can use this - see Johnn Blades answer); You fill a complete dataset while you only need a table, ....

my question is actually how to populate the combobox from the database. it brings error which says Procedure or function 'spSelectCustomerById' expects parameter '@id', which was not supplied. could somebody help me please

For web combobox us comboBoxEx1.DataValueField = "cust_id";
for WPF you use comboBoxEx1.SelectedValuePath = "cust_id";
in win forms use comboBox1.ValueMember = "cust_id";

my question is actually how to populate the combobox from the database. it brings error which says Procedure or function 'spSelectCustomerById' expects parameter '@id', which was not supplied. could somebody help me please.

 conn.Open();

        //SelectCustomerById(int x);
        comboBoxEx1.Items.Clear();
        SqlCommand comm = new SqlCommand("spSelectCustomerByID", conn);
        //comm.Parameters.Add(new SqlParameter("cust_name", cust_name));
        //comm.CommandText = "spSelectCustomerByID";
        comm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
        comm.CommandType = CommandType.StoredProcedure;
        comm.ExecuteNonQuery();
        SqlDataAdapter sdap = new SqlDataAdapter(comm);
        DataSet dset = new DataSet();
        sdap.Fill(dset, "cust_registrations");        
        comboBoxEx1.DataSource = dset;
        comboBoxEx1.DisplayMember = "cust_name";

// Add the following line
// You do not need to add items into the combobox.
        comboBoxEx1.ValueMember = "cust_id";
        comboBoxEx1.DataBind();


my question is actually how to populate the combobox from the database. it brings error which says Procedure or function 'spSelectCustomerById' expects parameter '@id', which was not supplied. could somebody help me please. i use this code for this but its not working..

if error is "@id", then why you passed the "@cust_id".
Try 
comm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
