combo box - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

combo box

j9070749j9070749 Posts: 7Member
Hi, I am creating a c# win form application.

I have 2 comboboxes.

One named suppliercombobox, the other named contactcombobox.

In my C# application I have a sql database linked in.

1 Supplier can have many contacts, one contact can only work for one supplier.

Below is the code which puts the values in the combobox suppliercombobox:

[code]
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblsupplier", con);

DataTable dt = new DataTable();
da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
SupplierComboBox.Items.Add(dt.Rows[i]["SupplierName"]);
}
con.Close();
}
[/code]

This code is called on the page load event, and works fine.

My code to put the values in the contact combo box is:

[code]

con.Open();

string supid = SupplierComboBox.SelectedIndex.ToString();
MessageBox.Show(supid);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblsuppliercontact WHERE supplierid= '"+ supid +"'", con);
DataTable dt = new DataTable();
dt.Rows.Clear();

dt.Clear();
da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++) // put a breakpoint here and look at the table it the datatable, it is the correct data but is not showing correclty in the listbox.
{
ContactcomboBox.Items.Add(dt.Rows[i]["ContactFirstName"]);
}
da = null;
dt.Rows.Clear();
con.Close();

[/code]

This code called when the text changes in the supplier combo box. Initially it will load the contacts for the correct supplier, but if I change the supplier again, the contact combo box will display the contacts from BOTH of the suppliers.

I have tried using dt.rows.clear(); and dt.clear(); in various places.

I would like to know how to get the contact combo box working as I descibed (to only show the contacts from the selected supplier).

I would also like to know how to display both the contacts firstname and surname in the combo drop down box.

I am very new to c# so unsure if I am approaching this correctly, please ask if you need to know anything else, any help is greatly appreiciated, I've been stuck on this a while now.

Many Thanks

Comments

Sign In or Register to comment.