我们需要通过从另一个组合框中选择值来将值添加到组合框。它没有按应有的方式选择值。只有第一个 if 部分执行。这是代码:它对我们不起作用。
private void section_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = (string)section.SelectedItem;
if(selected == "Giftarticles")
{
SqlConnection conn1 = new SqlConnection(connString);
conn1.Open();
string itemc = "(select distinct Itemcode from Items where Section1 like 'G%' )except(select distinct Itemcode from Items where Section1 like 'H%')";
SqlCommand cmditem = new SqlCommand(itemc, conn1);
SqlDataReader dr2 = cmditem.ExecuteReader();
while (dr2.Read())
{
itemcode.Items.Add(dr2["Itemcode"].ToString());
}
dr2.Close();
conn1.Close();
}
else
{
SqlConnection conn2 = new SqlConnection(connString);
conn2.Open();
string itemc1 = "(select distinct Itemcode from Items where Section1 like 'H%')except(select distinct Itemcode from Items where Section1 like 'G%')";
SqlCommand cmditem = new SqlCommand(itemc1, conn2);
SqlDataReader dr2 = cmditem.ExecuteReader();
while (dr2.Read())
{
itemcode.Items.Add(dr2["Itemcode"].ToString());
}
dr2.Close();
conn2.Close();
}
}