Just asking for a better solution for my approach in getting sql result with different multiple conditions based on comboboxes. I have a 4 comboboxes, for Branch, Type, Status and other info. User can select conditions from this comboboxes. The default value is "All", meaning no specific condition at all, but can choose from this cb's simultaneously. For example the user can choose specific type with all status and all other info, can also choose specific type, condition and all other info. in short he can select any combination of conditions from this cb's. I know I could query them individually but that would take me a lot of queries. Is there better approach for this


you want to fliter data from one combo box to another or can you put Sketch about your requirement

Make a stored procedure to which you can pass your variables.

yes I want to filter data from one combobox to another. I have already storeproc, but the problem is f i make a default parameter for each condition, it will require a value even if the combobox is selected as 'All', which in that case no condition should be for that matter. The 'All' there is just a dummy selection which will show data now without condition. Every combobox represents different columns in my sql server

What parameters are you using? If you are passing ID's you could send -1 for All.

to clarify it further, example

Branch selected, Type = 1, Status and other info = All, the query would be

Select * from table where Branch=@branch and Type = 1

branch selected, Type =1, Status = Pending, Other info = all, the query would be

Select * from table where Branch=@branch and Type=1 and Status = 'Pending'

branch selected, Type = All, Status = Complete, Other Info = Sent, the query would be

Select * from table where Status = 'Complete' and OtherInfo = 'Sent'

and so on,

So there are different combination of conditions from those cb's. doing it per possible combinations would take a lot of queries. Any better solution for this?

thanks again

Im using diffrent parameters for each cb. Some are ID's and some are Text

One way or the other, you'll have to build your query. Whether you do it in code, or in the stored procedure, there's no getting around it.

So that means no better approach for this? Do i need to query it individually?

Build a single query based on your variables.

In my query like
Select * from table where Branch=@branch and Type=1 and Status = 'Pending'
ho will I remove @branch parameter if the user wants to include all branches? not putting a value will generate error asking for the value of parameter since it is already included in my select statement?


Show your code, because I still don't know if you're using a stored procedure or a programming language.

Thanks for you guys for helping me, I did some revisions on my code. I declared a variable for my query, another variable for my conditions, then a just concatenate this variables depending on the conditions set by the user then pass this joined variable as my final query. It works for now since I have'nt encounter problems yet. Thanks again

Be a part of the DaniWeb community

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