ASP Lesson 3: Creating Dynamic Tables

Reporting is a necessary database function. What good would data be if we couldn't transform it into information? This lesson will illustrate some very basic reporting tactics.

What is a Dynamic Table?

Database connectivity is one of the key features of Active Server Pages. This allows us to create dynamic tables that are updated each time the page is requested. Through out this course, I will demonstrate various techniques for storing and retrieving data from a database.

During the later part of my college career, I thought I wanted to be a teacher. I developed the first iteration of the material presented in this online course for an independent study class. I had spent the previous semester teaching myself how to develop active content through the use of Active Server Pages. As you will quickly come to notice, nearly every example focuses on a particular aspect of building an online grade tracking application. If I was going to teach, I might as well have built myself some useful tools, since I was a student and had plenty of time on my hands.

As a professor, I would find it useful to create report for posting grades. I would have to do this anyway, if I wanted to keep my students aprised of their standing in the course. So, why not build a report once and let the web do all the work? As I enter grades into my online gradebook they become instantly available.

Basic Constructs for accessing a datasource

Before we start quering databases, there are some basic commands we need to add to our tool belt.

First of all, all Server-Side scripts need to be enclosed with either <% %> or <script RUNAT=Server> </script> tags. You must specify RUNAT=Server for the ASP engine to process the scripts.

If you leave out the RUNAT=Server, the ASP engine will assume the script is meant to run on the client. You may also specify Language=[VBScript|JavaScript]. If you do not specify a language, the default language set on the web server will be used. You can use both languages on the server, as long as the scripts are enclosed within their own <script> </script> blocks. For simplicity sake, I will be using VBScript throughout the course. The <% %> tags will also be used throughout, since I will be using the default language set on the server.

For those of you unfamilar with the SQL language, a brief introduction to each type of query will preceed a its use. Therefore, the syntax for a SELECT SQL query, which returns records from the data source, is as follows:

SELECT fieldname[,fieldname,...] FROM tablename

As you can see, you can specify any number of fields to be returned from the table, seperated by commas. If you want to select all fields from the table, use an astrisk (*) instead of field names.

To connect to a data source, we need to create a connection object and a recordset object. In the following example I will illustrate the first technique for connecting to our data source. Once a connection is established, a dynamic table will be generated. To view the sample output, click the button following the sample code.

Example 3.1

<html>
<head><title>Report 1</title>
</head>
<body>
<table border="1">
<tr>
   <td>Student ID</td>
   <td>Exam 1</td>
   <td>Exam 2</td>
   <td>Exam 3</td>
   <td>Final</td>
</tr>
<%
'this is the beginning of the server side script
set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "asp101"

'Next, assemble the SQL query string. You can
'extend a string over multiple lines by using
'the "_" character


sql="SELECT sid, exam1, exam2, exam3, final " & _
    "FROM Grades " & _
    "ORDER BY SID"

'Now it's time to execute our query.
rs.Open sql, cn

'iterate through the recordset until
'   the End Of File (EOF) is reached

do while not rs.EOF
%>

<tr>
   <%
   'write the value of a specified field in the
   '   current record
   %>

   <td><%response.write rs("sid")%></td>
   <td><%response.write rs("exam1")%></td>
   <td><%response.write rs("exam2")%></td>
   <td><%response.write rs("exam3")%></td>
   <td><%response.write rs("final")%></td>
</tr>
<%
  ' move to the next record
  rs.MoveNext 

loop

' close the recordset and connection objects
rs.Close    

cn.Close

' release the resources
Set rs = nothing 
Set cn = nothing
%>

</table>
</body>