Abstract
In my last article I reviewed
the ADO.NET object model as it is presented in the .NET framework Beta2.
To put this information to practical use, this article will provide a walkthrough
of how to use Microsoft's newest set of developer tools, found in Visual Studio.NET
to create a working Web Application that accesses an SQL database.
One of the important facts to note is that in this Beta2 release of VS.NET,
Microsoft have announced that there will be more than one version of the product
on final release. So far, they have announced Visual Studio .NET Enterprise
Developer and Visual Studio .NET Enterprise Architect. The latter product will
include additional features (that were in Beta1 to begin with), including:
- Software modeling
- Database modeling
- Development frameworks and templates
The example provided in this article showcases and makes extensive use of Visual
Studio.NET's visual designer capabilities, streamline the writing of code. On
to the example
Code Example
So, to bring all of this together we will be using the following ADO objects:
- DataAdapter (encapsulating
a SqlCommand)
- SqlConnection
- DataSet
- DataView
This example goes through creating a web application that connects to the Northwind
SQL database, executes a stored procedure and binds the results to a DataGrid
on an ASP.NET web page.
This is accomplished in three easy steps:
- Build the ADO.NET objects;
- Build the ASP.NET User Interface objects;
- Write the code behind the ASP.NET page (two lines!)
Ready? Here we go!
Building the ADO.NET Objects
1. Launch VS.NET Beta 2;
2. Create a new C# ASP.NET Web Application project:
Ready? Here we go!
Building the ADO.NET Objects
1. Launch VS.NET Beta 2;
2. Create a new C# ASP.NET Web Application project:

VS.NET will now create all the files required to get started
with this project.
3. Now bring up the Toolbox by clicking CTRL-ALT-X;
4. By default, the "Web Forms" Toolbox group will be displayed when
the Web Form is selected. Click on the "Data" Toolbox group just above
the Web Forms group:

5. Drag an SqlDataAdapter on to the Web Form
design surface:

The Data Configuration Wizard will appear:

6. Click Next;
7. Click "New Connection":

8. This will raise the "Data Link Properties" window. Fill it in as follows
and click OK:

The following window will appear:

Click Next;
9. In the page of the wizard that appears next, choose "Use Existing Stored
Procedures" and choose Next:

10. Under the "Select" statement, choose "Ten Most Expensive Products" and
click Next:
11. Click "Finish":

Note that at the bottom of the Web Form design surface, VS.NET
has created a tray which now contains an SqlDataAdapter
and SqlConnection objects:

12. Right click on one of the icons and choose "View Code":

This will bring up the associated code window for the page.
13. In the code window, find the line at the bottom that says "Web Form
designer generated code" and hover the mouse over it:

The collapsed code will now appear, revealing the code that
shows the syntax of how the SqlCommand and SqlConnection
objects are created.
14. Click on the tab at the top of the screen reads "WebForm1.aspx"
to get back to the designer:

15. Click once on the SqlDataAdapter icon and
press F4 to bring up the Properties window. At the bottom of the properties
window, click "Generate DataSet
":

16. Click "OK" on the "Generate DataSet" window that appears,
ensuring that the window appears as it does below:

VS.NET now generates the code for the DataSet
object and places an icon representing this object in the tray area of the Web
Form's design surface:

17. Select a DataView from the ToolBox and
drag it on to the designer:

This will cause a DataView placed on the tray
of the designer surface:

Like the SqlDataAdaptor, SqlCommand,
and DataSet objects, the DataView
has also been created in the code behind the page.
18. Click once on the DataView and in the Properties window, set its "Table"
property to the table in the DataSet:

This completes the configuration of the ADO.NET components for this application
- all done without writing any code!
Building the ASP.NET User Interface
19. From the Toolbox, click on the "Web Forms" tool group:

20. Now drag a DataGrid to the design surface
of the Web Form:

21. Position the DataGrid at the
top left of the page. Note that in VS.NET Beta2, rather than a "snap-to-grid"
feeling, the DataGrid can be dragged anywhere
on the page:

22. In the Properties window, set the DataGrid's
property to the DataView:

Note that when this property is set, the DataGrid
takes on the visual look of the data structure it will represent:

23. At the bottom of the Properties window, click on "AutoFormat",choose
the option "Colorful1" and click "OK":

24. Change the Font to "Arial":

The User Interface is now complete.
Writing the Code behind the ASP.NET Page
25. Press F7 to open the code behind the page;
26. Go to the Page_Load method by using the
right drop down list in the code window:

27. In the Page_Load method, enter the following
two lines of code:
sqlDataAdapter1.Fill(dataSet1);
DataGrid1.DataBind(); |
28. Save the project;
29. Press F5 to build and run in Debug mode. The following should appear
momentarily:

Conclusion
This article has reviewed the ADO.NET object model as it appears in the .NET
Framework Beta2, pointing out all of the key objects within that model. It has
also demonstrated the use of the model by means of the Visual Studio.NET development
environment, showing that a fully working web application can be easily created
with two lines of actual code typed in by the programmer.
A good place to go after this is the .NET
Quick Start ADO.NET Overview which goes into more depth about each of the
ADO.NET objects discussed in this article. Also, check out the other great articles
on www.dotnetjunkies.com
that will help you to make the most out of .NET!