AddThis Social Bookmark Button

Liberty on Whidbey

Data Binding in ASP.NET 2.0

by Jesse Liberty

In a previous article I demonstrated how to bind data within a Windows Form. In this article I will show how to bind data to ASP.NET controls. Not only has Microsoft made radical changes in how this is done between ASP.NET 1.x and 2.0, but it has also created significant differences between how this is done in Windows Forms and ASP.NET in 2.0. This keeps life interesting (he said, gnashing his teeth).

The code used in this example has been tested with the November Community Technical Preview of Beta 2 of Whidbey.

As the previous article noted, in the next version of the .NET Framework 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 Web Form that lets you page through the details of the Orders table from the Northwind database.

Start with the Fundamentals

Begin by creating a new ASP.NET application (call it ASPNW), which you will use to display information from the Northwind database. (For the location, choose File System and put it in whatever directory is convenient for you. The downloadable source code will use C:\WebSites\Oreilly\ASPNW.)

We'll be working with the Customers table in the Northwind database, so rename your aspx file from default.aspx to Customers.aspx. (Don't forget to change the class name both in the code file and in the page directive!)

Create a Data Connection

You need a connection to the database. You can explicitly create one, or you can use a control that depends on having a connection, and one will be created for you. Let's start by explicitly creating one. Drag a DataSource control onto the form, as shown in Figure 1:

Figure 1
Figure 1: Dragging a DataSource control

The SqlDataSource control will appear on your form. Click on the smart tag and choose Configure Data Source, as shown in Figure 2.

Figure 2
Figure 2: Using the smart tag

(If you do not see the SqlDataSource control, choose View -> Non Visual Controls.)

Clicking on the Configure Data Source option opens the wizard where you can choose your data source and data provider. Now things get tricky. When Visual Studio 2005 ships, the preferred database will be SQL Server or SQL Server Express. As of this writing, however, Microsoft has not yet provided Northwind for SqlServerExpress; so for this article we'll make a connection to Microsoft SQL Server. (Adjusting for Access is left as an exercise for the reader but is shown in the figure.) Figure 3 shows how to specify the data provider.

Figure 3
Figure 3: Specifying the data provider

Once you've chosen your data provider, you have the option to save the connection string in the application file. (The alternative is to save the connection string in the page as a property of the control.) The next step is to specify your query or to pick the columns you want from a specific table. For this example, you'll choose all the columns from the Customers table, as shown in Figure 4.

Figure 4
Figure 4: Choosing columns

While you are here, click on the Advanced button to see that you can instruct the wizard to generate the Update commands you'll need later in this chapter to update the database. For now, you can leave this unchecked. The next step in the wizard lets you test your query; clicking on Finish creates the connection.

In ASP.NET 2.0, the entire ADO.NET object model is abstracted into DataSource controls. That may be the most important sentence in this article. Every interaction with the database will be through one or more DataSource controls. Once you grok that, the rest is easy.

Pages: 1, 2, 3

Next Pagearrow