I have two combo box which names are cmbInvoiceNo
and cmbItemName
. I get all invoice numbers to the cmbInvoiceNo using following code.
// Get data from database to the **cmbInvoiceNo** combobox
private void GetInvoiceNo()
{
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select InvoiceNo From tblInvoice GROUP BY InvoiceNo", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string invoiceNo = dr.GetString(0);
cmbInvoiceNo.Items.Add(invoiceNo);
}
}
}
After that when I change the Invoice Numbers in cmbInvoiceNo combobox, my another combobox (cmbItemName) show items names according to the InvoiceNo. I set it to cmbInvoiceNo_SelectedIndexChanged
event like this.
private void cmbInvoiceNo_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbInvoiceNo.SelectedIndex == 0)
{
ClearAllFields();
}
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT ItemName from tblInvoice where InvoiceNo = '" + cmbInvoiceNo.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string itemName = (string)dr["ItemName"].ToString();
cmbItemNames.Items.Add(itemName);
}
}
}
It also working as expected. But my problem is after selected one invoice number I get all item names into cmbItemName combobox. When I select another Invoice Number my cmbItemName combox box show items with previous selected items. Its mean cmbItemName combo box repeat values. Can you please tell me how to show Items name without repeating item name. I want to show item name according to the invoiceNo. When invoiceNo change all item show is another combobox without prevoice value or repeat. Thank you. Please tell me how to avoid this problem.