WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button Liberty on Whidbey

ASP.NET 2.0 Databinding

by Jesse Liberty
11/08/2004

In ASP.NET 2.0, a great deal of the ADO.NET object model has been incorporated into controls that let you interact with data declaratively, and that spare you from writing boilerplate code to create data sets, extract tables, bind tables or views to controls, and so forth. In this article, you'll create a simple Windows form that lets you page through the details of the Orders table from the Northwind database. To begin, create a new Windows application called DataBinding.

Open the Data Sources Window (the menu choice Data -> ShowDataSources) and click on Add New Data Source... This opens the Data Source Configuration Wizard. You are free, using this wizard, to connect to a database, a local database file, a web service, or an n-tier design data object. In this case, we'll connect to a database.

The second step in the Wizard lets you choose a data connection. You can choose an existing connection, or you can create a new connection. If you create a new connection, you pick your server and decide whether you'd like to use Windows Integrated Security (Trusted_Connection=True) or you'd like to add your user ID and password. You then pick the database you want to connect to, and you can test the connection you've created, as shown in Figure 1.

Figure 1

Figure 1. Connection dialog

The next step allows you to save the connection string in the application's configuration file, which is a handy convenience. Once your connection is established, you are asked to select which objects you'd like to include in your DataSet, and to name that DataSet (a name is suggested for you). We'll include the Orders table, and take the proposed name, as shown in Figure 2.

Figure 2

Figure 2. DataSet configuration

Click Finish and a DataSet is created that contains a single table, Orders, with the fields you chose (in this case, all of the fields from the table in the database). You can now drag that table onto your form. The default is to create a GridView with each of the fields from the table. You can also click on the table (before dragging it onto the form) and an arrow appears that lets you change how the table will be presented on the form, as shown in Figure 3.

Figure 3

Figure 3. Adding detail data from the DataSource

After choosing Details, drag the table onto the form. A label and appropriate control is created for each field. A DataNavigator is added to the top of the form (see the arrow in Figure 4). In addition, the Component tray now has a DataConnector and a TableAdapter added.

Figure 4
Figure 4. Auto-created controls. Click image for full-size screen shot.

Table adapters are designer-generated components that connect your DataSet to the underlying data source. Table adapters are similar to DataAdapters, but are strongly typed and can contain multiple queries to support multiple tables.

DataConnectors act as a data source for controls; they are an intermediary between the DataSet and the control itself. The typical use for a DataConnector is to connect a group of data-bound-controls to a single table in a data source. This avoids the need to write the code to extract the table from the DataSet.

In addition to navigation, the DataNavigator provides CRUD operations (Create, Read, Update, Delete). You can delete the DataNavigator if you want to navigate through a UI you create yourself.

Finally, a line of code was added to the Form1_Load event handler:


private void Form1_Load(object sender, EventArgs e)
{
	// TODO: Delete this line of code to remove the default 
	// AutoFill for 'northwindDataSet.Orders'.
	this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);
}

This calls the Fill method on the designer-generated TableAdapter, passing in the table from which you want to fill. When you run the program, the data is filled, and the navigator can be used to move through the records. The data entry fields are all "alive" and appropriate to the data, as shown in Figure 5.

Figure 5

Figure 5. Live controls

Pages: 1, 2

Next Pagearrow