3

I have a GridView bound to an DataSource query with parameters tied to form controls.

What is the standard / best-practices way to handle optional query parameters?

2 Answers 2

5

Something like.. set the control parameter to -1 then in your query do something like...

Select * From Blah Where
(Somefield = @param or @param = -1)
Sign up to request clarification or add additional context in comments.

Comments

3

On your DataSource, have your ControlParam use the DefaultValue:

<asp:ControlParameter Name="CustomerID" 
    ControlID="DropDownList1" PropertyName="SelectedValue"  
    DefaultValue="-1" />

Then in your SQL statement, check for that same default value from your ControlParameter:

 SELECT * 
 FROM Invoices
 WHERE (CustomerID = @CustomerID OR @CustomerID = -1)

1 Comment

@pcampbell: great answer, we think alike

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.