Lab 3: ASP vs. ASP.NET
Visual Studio Team
Microsoft Corporation
August 2001
Summary: In this hands-on lab, you will build both an Active Server Pages (ASP) page and an ASP.NET page, each of which generates an HTML page using data from a database. (9 printed pages)
Download the Experience Visual Studio .NET Lab files from the introduction topic.
Contents
Introduction
Creating the ASP Page
Viewing the ASP page
Creating the ASP.NET Page
Viewing the ASP Page
Closing Out of Lab 3
Introduction
In this hands-on lab, you will build both an Active Server Pages (ASP) page and an ASP.NET page, each of which generates an HTML page using data from a database. In both examples, the concept of data access is the same—they both involve a connection to the database. The difference is in the way the data are collected and displayed.
Creating the ASP Page
- To open the Visual Studio .NET Integrated Development Environment, click Start, click Programs, click Experience VS .NET Content, click Lab 3, and then click ASP Source. A blank ASP page with the file name Authors.asp opens in Visual Studio .NET IDE, as shown in Figure 1.
Figure 1. ASP page
- Type the following code:
<%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <STYLE> BODY { font:arial } H1 { color:navy } </STYLE> </HEAD> <BODY> <DIV align=center> <H1>Authors</H1> <% ' ' Connecting to a database ' dim cn set cn = server.CreateObject("ADODB.Connection") cn.Open "Provider=sqloledb;" _ & "Data Source=(local);" _ & "Initial Catalog=pubs;" _ & "User ID=sa" ' Retrieving Data via the Recordset Object. dim rs set rs = server.CreateObject("ADODB.Recordset") rs.Open "select au_fname, au_lname, phone from authors order by au_lname",cn %>Note The mix of static HTML and server-side scripting in a table is in the following code. The recordset is iterated throughout in order to extract the data sequentially.
<TABLE border='1'> <TR> <TH>First Name</TH> <TH>Last Name</TH> <TH>Phone</TH> </TR> <% do until rs.EOF Response.Write "<TR>" Response.Write "<TD>" & rs("au_fname") & "</TD>" Response.Write "<TD>" & rs("au_lname") & "</TD>" Response.Write "<TD>" & rs("phone") & "</TD>" Response.Write "</TR>" rs.MoveNext loop %> </TABLE> <!-- Footer --> <h5>Current as of <%Response.Write now%></h5> </DIV> </BODY> </HTML> - Click File, and then click Save Authors.asp.
- Close the IDE.
Viewing the ASP page
- To view the ASP page, click Start, click Programs, click Experience VS .NET Content, click Lab 3, and then click ASP. The page shown in Figure 2 appears.
Figure 2. ASP page results
Creating the ASP.NET Page
- To open Visual Studio .NET IDE, click Start, click Programs, click Experience VS .NET Content, click Lab3, and then click ASP.NET VB Source. A blank ASP.NET page with the file name Authors VB.aspx opens in the Visual Studio .NET IDE, as shown in Figure 3.
Figure 3. ASP.NET page
- To view the source, click the HTML button in the lower left corner of the Visual Studio .NET IDE window.
- Type the following code:
Note The System.Data and System.Data.SqlClient namespaces are declared at the top of the page. All of the classes within these namespaces are available to the ASP.NET page.
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <STYLE> BODY { font:arial } H1 { color:navy } </STYLE> </HEAD> <BODY> <DIV align=center> <H1>Authors</H1>Note The server-side script is completely isolated from the static HTML. You can use any run-time language, such as Microsoft® JScript® and C#, in addition to Microsoft Visual Basic®.
<script language="VB" runat="server"> Sub Page_Load(Src As Object, E As EventArgs) Dim DS As DataSet Dim MyConnection As SQLConnection Dim MyCommand As SQLDataAdapter MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs") MyCommand = New SQLDataAdapter("select au_fname as 'First Name', au_lname as 'Last Name', Phone from Authors", MyConnection)Note The DataSet object in the following code replaces the Recordset object. Notice the fill method of the SQLDataAdapter object.
DS = new DataSet() MyCommand.Fill(ds, "Authors")Note See the following code sets the DataSource property of the DataGrid control. Note the Tables collection in the DataSet object; unlike Recordset objects, DataSet objects can contain more than one table.
grdAuthors.DataSource=ds.Tables("Authors").DefaultViewNote In the following code, the DataBind method of the DataGrid control loads the DataGrid with data. The DataGrid then displays the data as an HTML table.
grdAuthors.DataBind() End Sub </script>Note The following line of code embeds a DataGrid object in the page. Additonal properties of the DataGrid control can also be set by adding the property/value pairs. For example: Width="700" and BackColor="#ccccff".
<asp:DataGrid runat=server id=grdAuthors/> <!-- Footer --> <h5>Current as of <%Response.Write (Now.ToString)%></h5> </DIV> </BODY> </HTML>
- Click File and then click Save Authors VB.aspx.
- Close the IDE.
Viewing the ASP Page
- To view the ASP.NET page, click Start, click Programs, click Experience VS .NET Content, click Lab 3, and then click ASP.NET-VB. The page shown in Figure 4 appears.
Figure 4. ASP.NET page
Closing Out of Lab 3
When you have finished viewing the ASP.NET page, close all windows.
Other articles and labs in the Experience Visual Studio .NET set include:
Introducing the Visual Studio .NET Lab Series
Lab 4: Server Controls Walkthrough
Lab 5: Using the Visual Basic Upgrade Wizard