locked
remove filter in a sqldatasource RRS feed

  • Question

  • User1002836352 posted

    How can I remove filter in a sqldatasource .The following code doesn't work in a grid view although it works in a repeater.

    SqlDataSource2.FilterExpression = "";
    Friday, May 6, 2011 10:31 AM

Answers

  • User-1499637000 posted

    Hi,

    It should work, try setting to null or space.

    Here is the example where

    SqlDataSource1.FilterExpression = "";

    Is working fine for me.

    Example:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    
        protected void btnClearFilter_Click(object sender, EventArgs e)
        {
            SqlDataSource1.FilterExpression = "";
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>SQL DataSource Clear filter example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Choose Category :
            <asp:DropDownList ID="ddCategories" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource2"
                DataTextField="CategoryID" DataValueField="CategoryID">
            </asp:DropDownList>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
                DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                        ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="btnClearFilter" runat="server" Text="ClearFilter" 
                onclick="btnClearFilter_Click" />
        </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products]"
            FilterExpression="CategoryId='{0}'">
            <FilterParameters>
                <asp:ControlParameter ControlID="ddCategories" PropertyName="SelectedValue" Name="CategoryId" />
            </FilterParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT DISTINCT [CategoryID], [CategoryName] FROM [Categories] order by [CategoryID]">
        </asp:SqlDataSource>
        </form>
    </body>
    </html>
    

    Hope the above example helps to solve your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 6, 2011 2:30 PM