Question

I have this SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConflictDetection="CompareAllValues"
    ConnectionString="<%$ ConnectionStrings:OracleXexdb %>" 
    ProviderName="<%$ ConnectionStrings:OracleXexdb.ProviderName %>"
    SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR :col2 is null)"
     OnSelecting="SqlDataSource1_Selecting"
    >
    <SelectParameters>
        <asp:ControlParameter ControlID="codagent" Name="col2" PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" DefaultValue=""/>
    </SelectParameters>
</asp:SqlDataSource>

codagent is an <asp:TextBox> and the user can put a value or nothing (""), if the user leaves the TextBox with nothing, the SqlDataSource retrieves no values. My goal is allow user to get all the col2 values, without filter

Am I missing something?

Was it helpful?

Solution 3

I found the way to do this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConflictDetection="CompareAllValues"
    ConnectionString="<%$ ConnectionStrings:OracleXexdb %>" 
    ProviderName="<%$ ConnectionStrings:OracleXexdb.ProviderName %>"
    SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR (:col2 is null))"
    >

    <SelectParameters>
        <asp:ControlParameter ControlID="codagent" Name="col2" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" DefaultValue=""/>
    </SelectParameters>

</asp:SqlDataSource>

This worked fine

OTHER TIPS

Try changing your SQL to something like this

SelectCommand="select col1, col2, col3 from table where col1 < 2000 and (col2 = :col2 OR  (:col2 is null AND 1=1)"

I'm not really sure if such expressions are possible in Oracle since I don't have any experience with it but this is the logic that is used to accomplish the same thing in SQL Server.

Since you are already using the OnSelecting event of SqlDataSource, use this event to modify your Select command.

Also, since you want the user to retrieve all col2 values, in actuality, all such columns with col1<2000 will only be retrieved. [ Means, there can be col2 values corresponding to which col1> 2000, so such col2 will not be displayed at all ]

protected void SqlDataSource1_Selecting(object sender, 
SqlDataSourceSelectingEventArgs e) 
{ 
    if(string.IsNullOrEmpty(codagent.Text))
    {
      e.Command="select col1, col2, col3 from table where col1 < 2000";
    }
}

why don't you use subquery. following code is giving col2, col3 whether col1 is empty or not.

select (select col1 from table1 where col1 ='') as col1, col2, col3 from table1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top