Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

combo box

$
0
0
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:

            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]["SupplierNam
e"]);
            }
            con.Close();
        }


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

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


            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]["ContactFirst
Name"]);
            }
            da = null;
            dt.Rows.Clear();
            con.Close();



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

Viewing all articles
Browse latest Browse all 2703

Trending Articles