0

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 main category 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..

1 Answer 1

2

What is the context? WPF application?

If your tested request is ok, just use the string inside the way "you use like":

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 + "')";
SqlDataAdapter daSearch = new SqlDataAdapter(strQuery, conn);

You also made mistakes with the " and ' in your previous version.

For a better response, please update your question with: - info about your whole solution (type of application, environment) - info about your datamodel, because your request does not seem to be well written

EDIT 1 after your comments:

your problem is that you don't know how to get the value of the selected item in your comboBox. See here for difference between SelectedItem, SelectedValue... http://blogs.msdn.com/b/jaredpar/archive/2006/11/07/combobox-selecteditem-selectedvalue-selectedwhat.aspx?Redirected=true

Once you will be ready to read these values, create your query in a string like this

string myRequest = "SELECT companyName FROM table where mainCategory = '" + *value of mainCatU* + "' and subcategory = '" + *value of subCatU* + "'";

Then pass the string to SQL and get the results: SqlDataAdapter daSearch = new SqlDataAdapter(myRequest, conn);

Sign up to request clarification or add additional context in comments.

11 Comments

ah.. but then im using combobox , the dropdownlist was someone elses'.. i'll update it.. :) in a minute
also .. for combobox... example - my first combobox named mainCatU , so if i put it mainCatU.SelectedItem.Text ... the 'text' does not exist.. i only have SelectedItem.ToString , Equals and GetHype and GetHashCode
Please describe the type of project you are doing: comboBox in WPF and combobox in WinForms are not the same and do not have the same properties
if u need full details , i have actually setup a question before this , its the same thing , but different title.. stackoverflow.com/questions/20779276/…
Considering your question is only about the SQL request, use mainCatU.SelectedItem.ToString() and subCatU.SelectedItem.ToString() in my request edited previously to get the company names. Be careful when using those values: you should check if both comboboxes have selected values before calling this line (I don't know where you want to call this line)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.