2012-07-17 152 views
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"; 

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

+8

那么你的问题是什么? – 2012-07-17 09:16:06

+0

有什么问题? – 2012-07-17 09:16:51

+3

你为什么要做'comm.ExecuteNonQuery();'然后使用dataadapter填充数据集,你不需要'comm.ExecuteNonQuery();' – Habib 2012-07-17 09:20:47

回答

0

对于网络的ComboBox我们comboBoxEx1.DataValueField = "cust_id";

为WPF你赢形式使用comboBoxEx1.SelectedValuePath = "cust_id";

使用comboBox1.ValueMember = "cust_id";

+0

我的问题实际上是如何从数据库中填充组合框。它会带来错误,说明过程或函数'spSelectCustomerById'需要参数'@id',该参数未提供。请有人帮助我。 – yaki 2012-07-17 13:48:08

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(); 
+0

我的问题实际上是如何从数据库中填充组合框。它会带来错误,说明过程或函数'spSelectCustomerById'需要参数'@id',该参数未提供。请有人帮助我。我为此使用此代码,但它不工作.. – yaki 2012-07-17 13:48:51

+0

如果错误是“@id”,那么为什么你通过了“@cust_id”。 尝试
comm.Parameters.Add(new SqlParameter(“@ id”,SqlDbType.Int)); – 2012-07-17 16:10:54