Advanced Data-Binding Techniques

You can do a lot of things with data-bound controls. When you get out of the realm of small sample applications, you often start creating your own controls and creating multitier applications. The discussion of creating your own controls is better left to a book specializing on ASP.NET, such as ASP 2005 Unleashed, from Sams Publishing (0672328232).

When you begin adding tiers to your application, binding directly to a connected data source such as SQL Server or Oracle becomes impractical. In addition, you might be pulling data from a web service or from some other service provided by code written for the Windows Communication Framework. In these cases you don't want to bind directly to the data source for a number of reasons, not the least of which include potentially poor performance and scalability. To deal with multiple tiers or other situations where you want to bind to custom data, you can use an ObjectDataSource.

Creating and Utilizing an Object Data Source

Object data sources are data sources that read and write information to a class instance rather than to a connected source such as SQL Server or Oracle. To create an object data source, all you have to do is drag an Object Data Source onto your form and choose the class to which it will be bound.

This means that you'll have to create the class before you drag the object data source control. In an n-tier application scenario, this probably won't be difficult because many n-tier applications have business objects that supply strongly typed properties as well as methods to support the standard CRUD (Create, Retrieve, Update, Delete) functionality.

This sample will show you how to create an Object Data Source from a typed DataSet. This is the quickest way to get an object data source running from scratch because an object data source requires you to create your own implementation of the create, retrieve, update, and delete methods. After you have tried object binding with a typed DataSet, you can move on to implementing it with your own business objects.

The first step is to add a typed DataSet to your project. I created a typed DataSet called UsersDataSet, which has basic schema support for a string-based user ID, a first name, and last name. These users are stored in a SQL Express database on my machine. When you create a new DataSet in your solution, it will prompt you to choose a connection string and will allow you to automatically connect the DataSet to a database such as SQL Express. Although you don't have to connect your DataSet to a live source of data, it does make this sample easier.

With the DataSet created, you can then drag an ObjectDataSource component onto your Web Form. Use the wizard to select the data source. When you select the typed DataSet, the wizard will recognize the data object immediately and automatically select the appropriate methods for Select, Create, Update, and Delete operations.

Finally, with the ObjectDataSource created, you can drag a GridView component onto your form and select the object data source you just created as the data source.

Figure 27.8 shows the combination of the GridView control running against an ObjectDataSource. When you run this page, the GridView and the ObjectDataSource will enable a user to edit the contents of the data set, while persisting those same contents to a connected database, all without requiring you to write a single line of code.

Figure 27.8. A GridView and an ObjectDataSource working against a typed DataSet.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: