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:
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.
Add an ASP.NET Button control to the page.
Open the SqlDataSource control's Tasks menu and choose Configure Data Source.
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.
From the Column drop-down list, choose the Country field.
This sets
Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.