First I want to say Hello to all the great minds and the great thinkers in the world and Thank you in advance for any assistance you can provide. I appreciate being able to see farther standing on the shoulders of Giants and I'm a huge proponent of "Each One Teach One".So with that being said here's my problem. I have a Windows Form created in C# using Visual Studio 2012 Express that contains 3 Listboxes (Owner, Status, Type of Work) with multiple items in each Listbox and two textboxes (Date Assigned Start, Date Assigned End) that will serve as user input to create date range. What I'm trying so frustratingly to achieve is to take all of the Selected Items from each List box as well as the date range and use them to dynamically build a T-SQL Query that will be applied to an existing Data Set. To elaborate more Here's an example of what I'm trying to achieve and pardon me in advance if the example is long winded but I do believe in being thorough in order to achieve the desired results.
So, lets say I select Owners A,B,C then Status 1,2,3 then Type Of Work X,Y,Z then Start date 1/1/2013 and End Date 3/31/2013 I need to build an SQL Query that looks like this
Select <Columns> from <Table>
WHERE Owner ='A' AND Status ='1' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='1' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='1' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='2' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='2' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='2' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='3' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='3' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='A' AND Status ='3' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='1' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='1' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='1' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='2' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='2' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B' AND Status ='2' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B AND Status ='3' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B AND Status ='3' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='B AND Status ='3' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='1' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='1' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='1' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='2' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='2' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='2' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='3' AND [Type of Work]='X' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='3' AND [Type of Work]='Y' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
WHERE Owner ='C AND Status ='3' AND [Type of Work]='Z' AND DATEASSIGNED Between '1/1/2013' and '3/31/2013' OR
Now here's the kicker.......If there is no items selected for either Status, Type of Work or Date Assigned Date range (Owner will always have at least 1 item selected), I want to exclude those Items from the Query altogether. OBTW, I need this to happen on a Button_Click Event. Once the SQL String is constructed I will need it to execute on an existing DataSet and display the results in a DataGridView object on the same form. If anyone can provide me with some insight on how to achieve this it would be greatly appreciated and you will have done you great deed for the day and I will pay it forward in advance. Once again thanks for your time and effort. Take care.