Accessing and Updating Data in ASP.NET 2.0: Filtering Database Data with Parameters 01 Mar 2006 00:00 GMT| A Multipart Series on ASP.NET 2.0's Data Source Controls |
|---|
ASP.NET 2.0 introduced a number of new Web controls designed for accessing and modifying data.
These controls allow page developers to declaratively access and modify data without writing any
code to perform the data access. This article is one in a series of articles on ASP.NET 2.0's new data
source controls.
|
Data Source Control Basics - explores the concepts
and advantages of data source controls, and compares their usage in ASP.NET 2.0 to data access techniques in ASP.NET 1.x.Accessing Database Data - shows how to use the
SqlDataSource and AccessDataSource controls to query data from a relational database.Filtering Database Data with Parameters - learn how to
retrieve just a subset of database data based on hard-coded values and values from the querystring, other Web controls on the page,
session variables, and so on.
(Subscribe to this Article Series!
)
In Part 2 of this article series we looked at how to access
relational database data using the AccessDataSource and SqlDataSource controls. However, in Part 2 we only looked at how to
retrieve all of the records from a table, view, stored procedure, or ad-hoc SQL query. Often, we only want to retrieve
a subset of data based on various criteria. When searching for a book on Amazon.com, for example, the search page retrieves only
the data from the database that matches the search terms you entered; when viewing the details for a particular book, only
the particular book's information is queried from the database.
Results are filtered in a SQL statement using the WHERE clause, which contains a boolean expression. For each
record in the table being queried, the WHERE expression is evaluated to determine whether or not to include that
record in the resultset.1 For example, a SQL query like: SELECT ProductID, ProductName FROM Products WHERE
UnitPrice < 15.00 will return the ProductID and ProductName column values for all records
in the Products table for which the statement UnitPrice < 15.00 evaluates to True. In English, it
returns all products that cost strictly less than fifteen bucks.
The SqlDataSource and AccessDataSource controls can define the value by which the query is filtered by using parameters,
and these parameters can retrieve their values from a variety of sources, including: the querystring, session variables,
other Web controls on the page, and so on. Like the connection string and SELECT query, the filter parameters
values for a data source control can be set declaratively, meaning you do not need to write a line of code. These parameter
values can also be set programmatically, though, for cases where you might need to base the filter value on programmatic
logic (such as the currently logged-in user's username).
In this article we will examine how to declaratively filter the results of a SQL query using parameters in a SqlDataSource. We'll see examples
of how to use hard-coded filter values, and well as examples with filter values taken from the querystring and other Web controls
on the page. Read on to learn more!
1 - this is a gross oversimplification of how the database engine determines what rows, exactly, to return,
but is an accurate description from a high-level perspective...
Read More >
Source: 4GuysFromRolla