Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have an SQL data source on my aspx page that connects to the database and brings back data correctly. The issue I'm having is I'm trying to use a control on the aspx page to filter the data but I need to use the control in an SQL "OR" statement. The data is only picking up the first where condition of the "OR" statement. I also tried to union the Select statements and got the same results.

I've looked online and haven't found anything that pertains to my situation. Does anyone know if this can be done? Does anyone know how to accomplish this task or know where to find the answer?

I've included a sample of the connection string:

XML
<asp:SqlDataSource ID="sdsFiles" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
        SelectCommand="SELECT List.* FROM List WHERE (Name = ?) OR (Name LIKE '%Admin%') AND (Path LIKE '%' + ? + '\%')
                        ORDER BY Skipped"
        <SelectParameters>
            <asp:ControlParameter ControlID="lblUName" Name="?" PropertyName="Text" />
        </SelectParameters>


I also tried:

XML
<asp:SqlDataSource ID="sdsFiles" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
        SelectCommand="SELECT List.* FROM List WHERE (Name = ?)
        Union
         SELECT List.* FROM List WHERE (Name LIKE '%Admin%') AND (Path LIKE '%' + ? + '\%')
                        ORDER BY Skipped"
        <SelectParameters>
            <asp:ControlParameter ControlID="lblUName" Name="?" PropertyName="Text" />
        </SelectParameters>


Both give me the same results in that they only bring back the results for the first where condition.

Thanks,

Carolyn
Posted
Updated 12-Apr-11 7:23am
v3
Comments
web works 12-Apr-11 13:28pm    
Are you sure your query returns perfect result of your expectation. Please try to execute your query in SQL server and see it gives you the correct result. See if this helps you. Let me know.
CARisk3 12-Apr-11 13:31pm    
If I run the query with each individual where condition I get the results I expect. When I try to return it as one result with an "OR" or "Union" then I don't get what I expect. I even run the sql statement with the "OR" statement to figure out how many records it should be returning and it ran fine.

Carolyn

Try
SQL
SelectCommand="SELECT List.* FROM List WHERE (Name = ? OR Name LIKE '%Admin%') AND (Path LIKE '%' + ? + '\%')
 ORDER BY Skipped"
 
Share this answer
 
Comments
CARisk3 12-Apr-11 13:52pm    
That doesn't work. And the "Or" really needs to be grouped as I originally had it as I'm looking for the name only or I'm looking for where the name is like admin and the path as the name in it.

Thanks,
Carolyn
web works 12-Apr-11 14:09pm    
try this
"SELECT List.* FROM List WHERE (Name = ?) OR (Name LIKE '%Admin%') AND (Path LIKE '%" + ? + "\%') ORDER BY Skipped";
It depends on what you need, if you need:
- name like admin or name is the text in the control and if any of those is true, and the path is given then:
SQL
SelectCommand="SELECT List.* FROM List WHERE ((Name = ?) OR (Name LIKE '%Admin%')) AND (Path LIKE '%' + ? + '\%')
                        ORDER BY Skipped"

- name like admin and path is the text in control then return results or if those don't match then if user is the text from control then:
SQL
SelectCommand="SELECT List.* FROM List WHERE (Name = ?) OR ((Name LIKE '%Admin%') AND (Path LIKE '%' + ? + '\%'))
                        ORDER BY Skipped"
 
Share this answer
 
Comments
CARisk3 12-Apr-11 13:59pm    
I need to know if the Name field contains the same data as the control, if not I then need to check if the name field is like Admin and if the path field contains the same data as the control.

Neither of the above solutions work within the aspx page. It's only evaluating the first where statement.
Wendelius 12-Apr-11 14:01pm    
Ok, sorry, I misunderstood you. Have you tried adding the parameter twice?
<SelectParameters>
<asp:ControlParameter ControlID="lblUName" Name="?" PropertyName="Text" />
<asp:ControlParameter ControlID="lblUName" Name="?" PropertyName="Text" />
</SelectParameters>
CARisk3 12-Apr-11 14:03pm    
If I do that it says I can only have one parameter with the name.

Carolyn
Wendelius 12-Apr-11 14:06pm    
If you try replacing the question mark with parameter names. Something like:
...((Name = @Name1) OR (Name LIKE '%Admin%')) AND (Path LIKE '%' + @Name2 + '\%')

and

<SelectParameters>
<asp:ControlParameter ControlID="lblUName" Name="Name1" PropertyName="Text" />
<asp:ControlParameter ControlID="lblUName" Name="Name2" PropertyName="Text" />
</SelectParameters>
CARisk3 12-Apr-11 14:10pm    
No, it's still only evaluating the first where condition.

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