Performing Data Access

Overview of Data Access

Any web application worth writing involves data access. In this chapter, you learn how to take advantage of the rich set of controls included in the ASP.NET 3.5 Framework for working with data. You learn how to take advantage of the DataBound controls to display data in your ASP.NET pages. You also learn how to take advantage of the DataSource controls to represent different sources of data such as databases, XML files, and business objects.

Next, you are provided with an overview of Microsoft SQL Server 2005 Express, which is the royalty-free database included with Visual Web Developer. You learn how to
connect to this database and use it for all of your data access needs.


Using DataBound Controls
You use DataBound controls to generate your application’s user interface for working with data. The DataBound controls can be used to display and edit database data, XML data, or just about any other type of data you can imagine. There are three main types of DataBound controls: list controls, tabular DataBound controls, and hierarchical DataBound controls.

Working with List Controls
List controls are used to display simple option lists. The ASP.NET 3.5 Framework includes the following five List controls:
  • BulletedList— Displays a bulleted list of items. Each item can be displayed as text, a link button, or a hyperlink.
  • CheckBoxList—Displays a list of check boxes. Multiple check boxes in the list  can be selected.
  • DropDownList—Displays a drop-down list. Only one item in the drop-down list  can be selected.
  • ListBox—Displays a list box. You can configure this control so that only one item in the list can be selected or multiple items can be selected.
  • RadioButtonList—Displays a list of radio buttons. Only one radio button can be selected.


All five controls inherit from the same base ListControl class. This means that all these controls share a core set of properties and methods.

The page in figure illustrates how to use all five list controls to display the same set of database records.


Working with Tabular DataBound Controls
The tabular DataBound controls are the main set of controls that you use when working with database data. These controls enable you to display and, in some cases, modify data retrieved from a database or other type of data source.

There are six tabular DataBound controls. These controls can be divided into two types: those that display multiple data items at a time and those that display a single data item at a time.

First, you can use any of the following controls to display a set of data items:
  • GridView—Displays a set of data items in an HTML table. For example, you can use the GridView control to display all the records contained in the Movies database table. This control enables you to display, sort, page, select, and edit data.
  • DataList—Displays a set of data items in an HTML table. Unlike the GridView control, more than one data item can be displayed in a single row.
  • Repeater—Displays a set of data items using a template. Unlike the GridView and DataList controls, a Repeater control does not automatically render an HTML table.
  • ListView—Displays a set of data items using a template. Unlike the Repeater control, the ListView control supports sorting, paging, and editing database data.

You can use either of the following two controls to display a single data item at a time:
  • DetailsView—Displays a single data item in an HTML table. For example, you can use the DetailsView control to display a single record from the Movies database table. This control enables you to display, page, edit, and add data.
  • FormView—Uses a template to display a single data item. Unlike the DetailsView, a FormView enables you to layout a form by using templates.


The page in figure illustrates how you can use each of the tabular DataBound controls



Working with Hierarchical DataBound Controls
A hierarchical DataBound control can be used to display nested data items. For example, you can use hierarchical DataBound controls to display the folder and page structure of your website, the contents of an XML file, or a set of master/detail database records.

The ASP.NET 3.5 Framework includes two hierarchical DataBound controls:
  • Menu—Displays data items in a static or dynamic menu.
  • TreeView—Displays data items in a tree.

Using DataSource Controls
You bind a DataBound control to a DataSource control. A DataSource control is used to represent a particular type of data. The ASP.NET 3.5 Framework includes the following six DataSource controls:
  • SqlDataSource—Represents data retrieved from a SQL relational database, including Microsoft SQL Server, Oracle, or DB2.
  • LinqDataSource—Represents a LINQ to SQL query.
  • AccessDataSource—Represents data retrieved from a Microsoft Access database.
  • ObjectDataSource—Represents data retrieved from a business object.
  • XmlDataSource—Represents data retrieved from an XML document.
  •  SiteMapDataSource—Represents data retrieved from a Site Map Provider. A Site Map Provider represents the page and folder structure of a website.


The ASP.NET Framework contains two basic types of DataSource controls. The SqlDataSource, AccessDataSource, LinqDataSource, and ObjectDataSource controls all derive from the base DataSourceControl class. These controls can be used to represent tabular data. The XmlDataSource and SiteMapDataSource controls, on the other hand, derive from the base HierarchicalDataSourceControl control. These two controls can be used to represent both tabular and hierarchical data.

A DataBound control is associated with a particular data source control through its DataSourceID property.

Using ASP.NET Parameters with DataSource Controls
Many of the DataSource controls support ASP.NET parameters. You use ASP.NET parameters to modify the commands that a DataSource control executes. Different types of DataSource controls use ASP.NET parameters to represent different types of things. When you use ASP.NET parameters with a SqlDataSource control, the ASP.NET parameters represent ADO.NET parameters. In other words, they represent parameters used with SQL statements.

When you use parameters with the ObjectDataSource control, the ASP.NET parameters represent method parameters. They represent parameters passed to a particular method of a business object.

The SqlDataSource, AccessDataSource, LinqDataSource, and ObjectDataSource controls all support the following types of Parameter objects:
  • Parameter—Represents an arbitrary static value.
  • ControlParameter—Represents the value of a control or page property.
  • CookieParameter—Represents the value of a browser cookie.
  • FormParameter—Represents the value of an HTML form field.
  • ProfileParameter—Represents the value of a Profile property.
  • QueryStringParameter—Represents the value of a query string field.
  • SessionParameter—Represents the value of an item stored in Session state.

Using Programmatic DataBinding
When you bind a DataBound control to a DataSource control, you are taking advantage of declarative databinding. When you use declarative databinding, the ASP.NET  Framework handles all the messy details of deciding when to retrieve the data items represented by a DataSource control.

In certain situations, you’ll want to handle these messy details yourself. For example, you might want to force a GridView control to refresh the data it displays after you add a new record to a database table. Or, you might want to bind a DataBound control to a data source that can’t be easily represented by one of the existing DataSource controls. In these situations, you’ll want to use programmatic databinding.

Every DataBound control has a DataSource property and a DataBind() method. By using this property and method, you can programmatically associate a DataBound control with a data source.

Understanding Templates and DataBinding Expressions
Almost all the DataBound controls support templates. You can use a template to format the layout and appearance of each of the data items that a DataBound control displays. Within a template, you can use a DataBinding expression to display the value of a data item.

Using Templates
Every DataBound control included in the ASP.NET 3.5 Framework supports templates with the sole exception of the TreeView control. The Repeater, DataList, ListView, and FormView controls all require you to use templates. If you don’t supply a template, then these controls display nothing. The GridView, DetailsView, and Menu controls also support templates, but they do not require a template.


Using DataBinding Expressions
A DataBinding expression is a special type of expression that is not evaluated until runtime. You mark a Databinding expression in a page by wrapping the expression in opening <%# and closing %> brackets.

A DataBinding expression isn’t evaluated until a control’s DataBinding event is raised. When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.

Sample Database-Driven Web Application
The chapters that follow get into all the gritty details of the data controls. Before you get lost in the details, however, I want to provide you with a sample of a data-driven web application. I want to provide you with a “real world” application that illustrates what can be built with the data controls.

In this section, a complete Employee Directory application is built, which supports displaying, adding, editing, and deleting employee information. The sample application includes all the necessary form field validation.

One of the amazing things about the ASP.NET 3.5 Framework is how much the Framework simplifies data access. The sample application consists of a single page that contains very little code. Writing the same application with the ASP.NET 1.x Framework would require pages of code (I won’t even mention how much code it would require to write the same application in ASP Classic).

Because the Employee Directory application includes all the required validation code, the page is a little too long to include in the pages of this book. However, it is included on the CD that accompanies this book. Open the page named EmployeeDirectory.aspx. After you open the EmployeeDirectory.aspx page in your browser, you see a list of employees. This list is rendered by a GridView control.


Next to each employee, there is a Delete link and a Details link. If you click Delete, the selected employee is deleted from the database. Notice that a client-side confirmation dialog box appears when you click the Delete link (see Figure 8.19). This dialog box is added to each of the Delete links in the grdEmployees_RowCreated() method. This method is called automatically by the GridView control as the GridView creates each row.

If you click the Details link, a window appears that displays detailed information for the Employee (see Figure). The detailed information is rendered by a FormView control. The window that appears is created with an absolutely positioned <div> tag. If you click Edit when viewing a employee’s details, you can edit the employee record. The edit form is contained in the FormView control’s EditItemTemplate. Each of the form fields is associated with a RequiredFieldValidator control.


Finally, you can add new employees to the directory by clicking the Add Employee button. The form that appears is also rendered by a FormView control

No comments:

Post a Comment