6.4. Using Parameters in Queries

When you set up the SqlDataSource control's SelectCommand (see the earlier section, "Adding and configuring a SqlDataSource control") you told it to fetch all the customers. You probably don't want to deal with that much data at one time because the more records, the more paging. The preferable method is to tell the SqlDataSource control to limit the scope of the data. You filter the data by passing one or more parameters. As you see in this section, you can get parameter values from many sources.

6.4.1. Getting a parameter value from a TextBox control

Say you want to filter the database query on the starter.aspx page to show only customers from a given country. You only know the country name at runtime so you can't hardcode the name. The easiest way is to let the user type the country name and use that value as part of the query. Follow these steps to pass a parameter value from an ASP.NET TextBox control to the SqlDataSource control:

  1. From the Toolbox (in the Standard category), add an ASP.NET TextBox to the page (above the DetailsView control) and set its ID property to txtCountry.

  2. Add an ASP.NET Button control to the page.

  3. Open the SqlDataSource control's Tasks menu and choose Configure Data Source.

  4. In the Configure Data Source Wizard, click the Next button to step to the Configure the Select Statement screen and click the WHERE button.

    The Add WHERE Clause dialog box appears.

  5. From the Column drop-down list, choose the Country field.

    This sets

Get ASP.NET 3.5 For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.