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()
        {
            InitializeComponent();
        }
        private void User_Load(object sender, EventArgs e)
        {

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

            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> 
                {
                 dr["Subcat1"].ToString(), 
                 dr["Subcat2"].ToString(),
                 dr["Subcat3"].ToString(),
                 dr["Subcat4"].ToString()
                };
                Category.Add(dr["mainCate"].ToString(), SubCats);
                mainCatU.Items.Add(dr["mainCate"].ToString());
            }

            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;
            daSearch.Fill(dt1);
            ListU.ValueMember = "cName";
            ListU.DisplayMember = "cName";
            ListU.DropDownStyle = ComboBoxStyle.DropDownList;
            ListU.Enabled = true;

//**----------------------**

            conn.Close();   
        }

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

and the databases are as shown :

dbo.MAINCATE

dbo.ComDet

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";
            conn.Open();

            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();
            daS.Fill(dts3);
            dataGridView1.DataSource = dts3.DefaultView;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            conn.Close();

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

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.

This article has been dead for over six months. Start a new discussion instead.