I have created a form , with a working coding , but i need an extra assistance.

Somewhat , i am stuck on trying to figure out how to let the 3rd combobox value , determined by the first and second.

and what i wanted is , something like , getting the value of Main Category and Sub category to effect the list of the third combo box.

i just need the SQL query , such as : "SELECT companyName FROM table where maincategory = firstcombobox and subcategory = secondcombobox"

and then shows the company name within the picks of main and sub category.

Like this :

Main Form

For the Main Category and Sub-Category , i have it working with this code.
This code also includes the 3rd ComboBox code which right now operates without the first and second combobox attached to the 3rd combobox code.

public partial class User : Form

        Dictionary<string, List<string>> Category = new Dictionary<string, List<string>>();

        DataSet ds1;

        public User()
        private void User_Load(object sender, EventArgs e)

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";

            SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn);
            ds1 = new DataSet();

            daMain.Fill(ds1, "Maincate");

            DataTable dt = ds1.Tables["MAINCATE"];
            foreach (DataRow dr in dt.Rows)
                List<string> SubCats = new List<string> 
                Category.Add(dr["mainCate"].ToString(), SubCats);

            mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
            mainCatU.Enabled = true;
            subCatU.DropDownStyle = ComboBoxStyle.DropDownList;

//**Code for third combobox**

            SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
            DataTable dt1 = new DataTable();
            ListU.DataSource = dt1;
            ListU.ValueMember = "cName";
            ListU.DisplayMember = "cName";
            ListU.DropDownStyle = ComboBoxStyle.DropDownList;
            ListU.Enabled = true;



        private void mainCatU_SelectedIndexChanged(object sender, EventArgs e)
                subCatU.DataSource = Category[mainCatU.SelectedItem.ToString()];

and the databases are as shown :



and the code for the View Selected Company button is :

private void searchBtn_Click(object sender, EventArgs e)
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";

            SqlDataAdapter daS = new SqlDataAdapter("select cName, cDetails, cDetails2 from ComDet where cName = @cName", conn);
            daS.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;

            DataTable dts3 = new DataTable();
            dataGridView1.DataSource = dts3.DefaultView;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;


  • again , right now the third combo box is coded to run without the maincategory and subcategory

Although elaborate, your question is not very clear.

To put it into short story

i wanted something like "SELECT companyName FROM table where mainCategory = firstcombobox and subcategory = secondcombobox" , how do i do the sql query?

I guess LINQ to SQL might help you out here. Helas, I'm not proficient enough in LINQ to help you out here. But another DaniWeb member might.