0

I have a form with 3 combo boxes. The value of the combo boxes generates a SQL query with 3 parameters. The SQL generates a RDLC report.

For example:

Combox1 with values(v1,v2,v3,ALL)

Combox2 with values(v1,v2,ALL)

Combox3 with values(v1,v2,v3,4,ALL)

Me.ExampleTableAdapter.QueryWithParams(Me.ExampleDataSet.Table, Combobox1.SelectedIndex, Combobox2.SelectedIndex,Combobox1.SelectedIndex)

QueryWithParams:

SELECT * FROM Table WHERE Column1=@c1 AND Column2=@c2 AND Column3=@c3

Ok, the problem is when the selected value is ALL in the combo X, I cannot get the values because I must change the query and kick out the parameter. For example: If the combo1, combo2, combo3 have the values ALL. I don't need parameters.

SELECT * FROM Table 

If the combo1 is ALL.

SELECT * FROM Table WHERE Column2=@c2 AND Column3=@c3

If combo1 and combo3 are ALL

SELECT* FROM Table WHERE Column2=@c2 

... Then in code I look which is the selected value of the combo and upon it I do the query.

Is there a way that i do this thing without generating all 8 types of queries?

PS. I cannot use stored procedures because I'm using SQL Server Compact

2 Answers 2

1

If the value for 'ALL' should be sent to the query with value 'NULL'

SELECT * 
FROM Table 
WHERE (Column1=@c1 OR @c1 IS NULL)
AND (Column2=@c2 OR @c2 IS NULL)
AND (Column3=@c3 OR @c3 IS NULL)

Edit: Of course same can be done for any specified 'ALL'-value Example of 'ALL' = -1

(Column1=@c1 OR @c1 = -1)
Sign up to request clarification or add additional context in comments.

Comments

0

You should be a ble to use IsNull in your query for the columns where nothing/all was selected:

WHERE Column1 = ISNULL(@c1, Column1)

MSDN

Comments

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.