Bind to Data Visually in ASP.NET
Instead of digging deep into the code-behind class to build data-bound ASP.NET pages, do almost everything visually.
by Jonathan Goodyear
March 2003 Issue
Technology Toolbox: VB.NET, C#, ASP.NET
Most tutorials about data binding in ASP.NET require you to build lots of code in the code-behind class. This method gives you ultimate control over the way you retrieve and display your data, but you can accomplish quite a bit using a purely visual approach in Visual Studio .NET. This article walks you through a visual data binding exercise. It's not meant to be an exhaustive tutorial on visual data binding in VS.NET, but it will definitely get you started on the right track.
To start off, create a new ASP.NET Web application. It doesn't matter whether you write it in C# or Visual Basic .NET. Drag a OleDbDataAdapter control from the Data section of the toolbox onto the design surface of the default Web form that is created with the project. Click on the Next button when the Data Adapter Configuration wizard appears.
You can select from a data connection that has been created already, but for now, select the New Connection button. A standard Universal Data Link (UDL) wizard is displayed. Fill in the proper server and connection credentials to connect to the Pubs database (see Figure 1) and click on OK. If you select the "Allow saving password" checkbox, you get a warning message stating that your password won't be encrypted. Don't worry about that for the purposes of this exercise. Click on Next to proceed.
Next, you need to choose how you'll retrieve data from your data source (the Pubs database, in this case). You can use a SQL statement, create a new stored procedure, or select from an existing stored procedure. For this exercise, choose the SQL statement option and click on Next. You're presented with a textbox in which to type your SQL query. A Query Builder button launches a Query By Example (QBE) wizard to help you build your query. Click on the Query Builder button and enter a query to select the first and last names of the authors in the authors table (see Figure 2). Click on OK on the Query Builder window. The query that you just built should now be displayed in the query box.
Click on the Advanced Options button. Deselect the top checkbox labeled "Generate Insert, Update, and Delete statements." Your author list isn't editable, so these objects are not necessary. The second and third checkboxes are grayed out. Click on the OK button, followed by the Next button in the wizard. You'll see a summary page for the Data Adapter Configuration wizard, outlining everything it's done for you. Click on the Finish button to return to the design surface of your Web form.
Back to top
|