Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a customer who would like to filter records based on fields associated with that table. E.g. If there are over 2000 Employees, we want the user to say where (Employee Status = <value> and Employee Gender = <value>) OR(Employee Position = <value>). You'll notice the <values> are foreign keys to another table.

There's aproxmiately 5 different screens in which similar functionality will be required (e.g. Orders, Company, Product, etc..)

Any ideas how we can implement such a solution and allow the end user to build the logical AND OR with paranthesis ()?

Thank you.
Posted
Updated 15-Jun-11 5:23am
v2

It's called an ad-hoc query. There are a million and one ways to implement something like that. Google is your friend.

716,000 google results for "asp.net ad-hoc query"[^]

I saw a link to codeplex in there, and if you add "codeproject" to the search phrase, you'll likely find something there as well.
 
Share this answer
 
v2
I recommend NOT allowing your users to manually enter ad hoc SQL or SQL-like queries. I speak from experience here: that road leads to madness.

Make a list of searchable columns; Employee might have first name, last name, status, gender, branch office, position and hire date. Create a filter form where the user can enter complete or partial information. When possible, pre-populate your options: gender can be coded to be a drop-down with F, M and blank (which would be no filtering) while branch office could be drop-down populated from the database.

When the user clicks on the Search button, your code then assembles the form data into a WHERE clause. If an input is blank, then there is no filtering on that data point. For example, you would include BranchOffice=@BranchOffice if the user selected one from the drop-down, or LastName LIKE @LastName if LastNameFilter.Text is not empty, with the parameter value set to LastNameFilter.Text + %. Perform your query and return the results.

It will take some work to set up, but it will be much less work than trying to write a command parser and teaching your users how to use it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900