我有2个下拉列表,它是类别和产品名称。我可以根据所选的类别填充产品名称列表中的值。现在我必须根据所选的产品名称填充数据库中的价格和可用数量等其他字段。
这是类别下拉列表的代码
private void makerOrderFrm_Load(object sender, EventArgs e)
{
string selectCommand = "select distinct(productCategory) from
productsTbl";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(selectCommand, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
cmbCategory.Items.Add(reader[0].ToString());
}
reader.Close();
conn.Close();
}
catch (SqlException)
{
MessageBox.Show("can't show categories");
}
这是产品名称下拉列表的代码
private void cmbCategory_SelectedIndexChanged(objectsender,
EventArgs e)
{
conn.Close();
cmbProdName.Items.Clear();
string selectCommand2 = "select productName from productsTbl
where productCategory=@productCategory";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(selectCommand2, conn);
cmd.Parameters.AddWithValue("@productCategory",
cmbCategory.Text);
SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
cmbProdName.Items.Add(reader[0].ToString());
}
//cmbCategory.SelectedIndex = 0;
reader.Close();
}
catch (SqlException)
{
MessageBox.Show("can't show categories");
}
}
现在我无法根据所选产品名称获取价格,可用数量等字段
private void cmbProdName_SelectedIndexChanged(object sender,
EventArgs e)
{
string selectCommand2 = "select productPrice,productQuant
from productsTbl where productName=@productName";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(selectCommand2, conn);
cmd.Parameters.AddWithValue("@productPrice",
txtProductPrice.Text);
cmd.Parameters.AddWithValue("@productQuant",
txtAvblQty.Text);
}
catch (SqlException)
{
MessageBox.Show("can't show categories");
}
}