0

我的场景是从数据库中填充组合框,显示客户姓名,并使用身份增量保存 id 的值。

运行此代码时,我收到一个错误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";

如何从数据库中填充组合框?

4

2 回答 2

0
 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";

// 添加以下行 // 您不需要将项目添加到组合框中。

        comboBoxEx1.ValueMember = "cust_id";
        comboBoxEx1.DataBind();
于 2012-07-17T09:24:11.840 回答
0

对于网络组合框我们comboBoxEx1.DataValueField = "cust_id";

对于您使用的 WPFcomboBoxEx1.SelectedValuePath = "cust_id";

在获胜形式中使用comboBox1.ValueMember = "cust_id";

于 2012-07-17T09:18:13.153 回答