I have 3 comboBox..

and i wanted a code for the thirdcombobox , something like

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

my maincategory combobox is called mainCatU , and the subcategory is subCatU

I managed to make subCatU's value based on mainCatU , now i wanted a third combobox value determined by both the value of the maincategory and subcategory.

it is either just an SQL Query or other thing?

can anyone help?

i have tried following some other codes such as

string strQuery = "SELECT * FROM Purchase where ItemID=(SELECT ItemID FROM ItemMaster where ItemName='" +  DropDownList3.SelectedItem.Text + '" and CategoryID=(SELECT CategoryID FROM ItemMaster where ItemName='"+ DropDownList3.SelectedItem.Text + '")"; 

But i do not use that.. since i am using it like this..

SqlDataAdapter daSearch = new SqlDataAdapter("SELECT companyName FROM CompanyDetail", conn);

help please..

Salam Mohd,

I don't think it is a proper way to query from database every time the user changes the combobox values.

Instead you can use your SqlDataAdapter to fill a dataset, something like this:

DataSet myDataset = new DataSet();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT companyName FROM CompanyDetail", conn);
daSearch.Fill(myDataset);

After that you need to bind your third combobox to the resulted table inside 'myDataSet':

BindingSource  = new BindingSource();
thirdcomboboxBindingSource.DataSource = myDataset.Tables[0];

thirdcombobox.DataSource = thirdcomboboxBindingSource;
thirdcombobox.DisplayMember = "column_name"; // where 'column_name' is the name of the column you want to display in your combobox.

Final step is to filter the results based on the current values of the two comboboxs' values:

thirdcomboboxBindingSource.Filter = string.Format("ItemName = {0} AND CategoryID = {1}",
                                        firstcombobox.SelectedValue.ToString(),
                                        secondcombobox.SelectedValue.ToString());

Now you need to make you change the filter each time the first or second comboboxs inside SelectedValueChanged event handler.

Hope this would help.

I also feel personally that you should try following The Diamonds suggestions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.