I use SqlDataSource in a very simple application. I allow the user to set several search parameters for the SDS select command via TextBoxes, one TextBox for each parameter (think txtFirstName, txtLastName, etc.). I plan to use a button click event handler to set the SqlDataSource SelectCommand property, which by default will return all records (for my purposes here). I want to refine this select command in order to add one or more WHERE clauses depending on whether the user enters search criteria in any of my text fields.
An example in case I do not understand:
By default, the SqlDataSource SelectCommand property will look something like this:
SELECT * FROM MyTable
If the user enters "Bob" in txtFirstName, I want the SelectCommand property to look like this:
SELECT * FROM MyTable WHERE [FirstName]='Bob'
If the user enters "Jones" in txtLastName, I want the SelectCommand property to look like this:
SELECT * FROM MyTable WHERE [FirstName]='Bob' AND [LastName]='Jones'
My question is:
Is there a way to dynamically create these WHERE clauses without me, to check for empty text fields and construct the WHERE clause manually?
My small application has only three parameters, so the rough way to get me through this will not be painful, but I wondered if there is an easier way to do this plus, maybe I will need to add additional parameters to the future. In addition, I can add wildcard search.
source share