两个组合框-cmbSupplier和cmbProduct
我想用表中的供应商名称填充cmbSupplier
当我从cmbSupplier中选择一个值时 取决于选择的名称 具有供应商名称的产品应填写cmbProduct
私有无效frmAddOrder_Load(对象发送者,EventArgs e) {
SqlCommand cmd = new SqlCommand("SELECT sup_name from tbl_supplier",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
cmbSupplier.Items.Add(dr["sup_name"].ToString());
}
dr.Close();
}
这是我用来填充cmbSupplier的代码
我尝试使用此代码填充cmbProducts,但我得到的只是一个错误
private void cmbSupplier_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT Product_Name from tbl_SupplierItems WHERE supplier_name'" + cmbSupplier.Text + "%'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
cmbProduct.Items.Add(dr["Product_Name"].ToString());
}
dr.Close();
}
答案 0 :(得分:-1)
查询不正确。 代替
SqlCommand cmd = new SqlCommand("SELECT Product_Name from tbl_SupplierItems WHERE supplier_name'" + cmbSupplier.Text + "%'", con);
您应该写
SqlCommand cmd = new SqlCommand("SELECT Product_Name from tbl_SupplierItems WHERE supplier_name = '" + cmbSupplier.Text + "%'", con);