New features for web developers in ASP.NET 2.0

Working with Data

This article was originally published on DNJ Online
DNJ Online

ASP.NET 2.0 provides a huge number of new features that increase developer productivity, remove complexity, and make building great Web sites much easier. Much of the ‘plumbing' (the repetitive coding) that is required in version 1.0 has been replaced by innovative new controls and enhancements to the underlying page architecture. That said, most version 1. x pages will run with only minor modifications.

Working with Data

One of the most common tasks in most Web pages is working with data. In many cases the data comes from a relational database such as SQL Server, and the developer must write repetitive code to access it, display it, and push changes back into the database. In version 1. x , server-side data binding to a range of list controls reduces the amount of rendering code required. However, in version 2.0, new data source controls such as the SqlDataSource control remove the need to write any code at all for common read and update scenarios.

There are data source controls that can handle relational and XML data, and a data source control that allows you to use a separate business logic or data access layer to read and update data in an n -tier application. There are also new user interface (UI) controls that interact with the data source controls to display and allow editing of data exposed by the new data source controls. These include an updated DataGrid control named GridView, a new DetailsView control that displays data one row/record at a time, and a free-form control named FormView that uses templates to create the visible display and editing UI.

As an example, the listing below shows the complete set of ASP.NET control declarations required to display some rows from a database table in a Web page. You can see that all it contains is an SqlDataSource control and a GridView control. When the page is executed, the controls respond to events raised by ASP.NET to retrieve the rowset and display the contents. No other code is required.

<asp:SqlDataSource id="ds1" runat="server"
  ConnectionString="Server=.;Database=Northwind;
                      Trusted_Connection=True"
  SelectCommand="SELECT CompanyName, Address, City,
                   PostalCode, Country
                   FROM Customers
                   WHERE Country LIKE ' Brazil'" />
<asp:GridView id="grid1" runat="server" DataSourceID="ds1" /> 

The result is shown below, along with an example of the DetailsView and the FormView controls displaying more rows from the sample Northwind database.

As well as automatically supporting inline editing, deleting and adding new rows, the combination of the new data source and the new display controls support paging and sorting - and all without any code being required.

The data source controls are also complemented by a series of new controls that allow you to select and filter rows using values that are automatically taken from other controls on the page, from a cookie, or directly from the Form or QueryString collections. Furthermore, the new data source controls can also be used with existing controls such as the ListBox, DropDownList, RadioButtonList and so on.

The GridView control effectively replaces the DataGrid control from version 1. x , although this is still available and can be used just as in version 1. x . If your existing applications already use the DataGrid, there is no point in changing to the GridView when you migrate to version 2.0. However, for new applications, the GridView really does make development much easier. It supports all the existing features of the DataGrid; for example you can turn off auto-generation of columns and specify your own column set, handle events to perform custom paging, sorting and selecting rows, and use templates to create the UI, in the same way as in a DataGrid.

You can also handle the RowDataBound event (the equivalent to the DataGrid control's ItemDataBound event) to modify the output in each row, plus there are plenty of new events such as RowCreated, RowDeleted and RowUpdated that allow you to interact with the control as the user edits the rows it contains. And the DetailsView and FormView controls expose a similar interface to the GridView, so working with a combination of these controls to create a master/details view of data that makes it easy to insert new rows, for example, is quite intuitive.

Furthermore you can take advantage of SQL Cache Invalidation in ASP.NET 2.0 when caching data from a SQL Server database (version 7.0 and above). This new feature allows you to cache data for long periods, and be notified by the database only when that data changes. For SQL Server 7.0 and SQL Server 2000, this feature is enabled though a utility supplied with ASP.NET. For SQL Server 2005 you use the built-in Broker Service instead. (For more details see 'SQL Server Query Notifications'.)

You might also like...

Comments

About the author

Alex Homer

Alex Homer United Kingdom

Alex spent most of his earlier working life as a technical salesman, and has had a love-hate relationship with computers that goes way back to the Sinclair Z80 and the Oric Atmos. In 1996 he ret...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond