0

私のシナリオは、データベースからコンボボックスを作成し、顧客名を表示し、ID の増分を使用して 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";

使用するWPFの場合comboBoxEx1.SelectedValuePath = "cust_id";

winフォームで使用comboBox1.ValueMember = "cust_id";

于 2012-07-17T09:18:13.153 に答える