This documentation is archived and is not being maintained.

How to: Enable Filtering for SqlDataSource Controls (Visual Studio)

Visual Studio 2005

The SqlDataSource control allows you to filter (sort or select) the results of a query without rerunning the query. By adding filtering to a SqlDataSource control, you can change what data is made available by the SqlDataSource control after a query has been run, without returning to the database.

This topic shows you how to enable filtering for a SqlDataSource control. Data-bound controls that are bound to the SqlDataSource control, such as a GridView control, will show only the filtered results.

To use filtering, you must set the SqlDataSource control to return information in a dataset and to cache its results. You can then specify a filter expression that is applied as a RowFilter property to the DataView object that underlies the SqlDataSource control.

The filtering can also include parameters that are based on the values of another control, cookies, sessions, or query strings. For example, if a DropDownList control contains city names, you can use the selected city as your filtering parameter.


Creating a filter expression with parameters is not the same as creating a parameterized SelectCommand property and specifying SelectParameters property values. A parameterized filter applies different views of the data to a cached dataset. A parameterized select command performs a query against the data source.

To enable filtering for a SqlDataSource control

  1. In Design view, from the Data tab of the Toolbox, drag a SqlDataSource control onto the page.

  2. Right-click the control and click Show Smart Tag.

  3. In the SqlDataSource Tasks menu, click Configure Data Source.

  4. Follow the Configure Data Source wizard to create or choose a connection and to create a query to return data from the data source. For details, see Walkthrough: Basic Data Access in Web Pages.

  5. In the Properties window, set the DataSourceMode property to DataSet.

  6. Set the EnableCaching property to true.

  7. Set the CacheDuration property to the number of seconds that you want the data to be cached. The number you choose depends on your application.

  8. Set the FilterExpression property to an expression specifying the data to return, as shown in the following example expression.

    city = 'Seattle'

    For information about the syntax of filter expressions, see RowFilter.

Filtering with Parameters

At times, you might want to filter the results of a query based on values that are known only at run time. You can create filter expressions that include parameter placeholders, and then define filter parameters to fill the placeholders. The filter parameters can get their values from controls, query strings, cookies, session variables, profile properties, or Form property values.

To filter a SqlDataSource control using parameters

  1. In Design view, click the SqlDataSource control to which you want to add filtering parameters.

  2. In the Properties window, set the FilterExpression property to an expression that includes a placeholder for the filter parameter value. The placeholders use the syntax {n}, where n indicates the sequential order of the parameter.

    The following examples show parameterized filter expressions. The second expression includes multiple parameter placeholders.

    category = '{0}
    state = '{0}' AND city = '{1}'

    For information about the syntax of filter expressions, see RowFilter.

  3. In the Properties window, click the ellipsis button next to FilterParameter.

  4. In the Parameter Collection Editor dialog box, click Add Parameter.

  5. In the Name field, type the name that you want to assign to the parameter.

    The Name property for the parameter is required. However, parameters are matched to placeholders in sequential order, not by name.


    To edit the name after creating the parameter, click the name and then press F2 to put the name into editing mode.

  6. In the Parameter source drop down list, select the appropriate source for the parameter, such as a control on the Web page.

    Depending on which parameter source that you have selected, the dialog box changes to include a way to specify the ID of the property value.

  7. If you selected Control as the parameter source, in the ControlID drop-down list, select the ID for the control that will provide the value for the parameter. Optionally, add a default value for the parameter; and then click OK.

  8. Repeat steps 4 through 7 for each parameter that you want to create.

  9. When you are finished creating parameters, click OK to close the Parameter Collection Editor dialog box.

See Also