Script Junkie
"A Database Framework With ASP"
by  Marco Tabini
Web Techniques, June 1998

Web Techniques grants permission to use these listings (and code)  for 
private or commercial use provided that credit to Web Techniques and 
the author is maintained within the comments of the source. For 
questions, contact editors@web-techniques.com. 

This file consists of the two listings accompanying Marco Tabini's article in Web Techniques Magazine, titled "A Database Framework With ASP ".

For the complete project, see tabini.zip.



 [LISTING ONE]

<%
   ' File:      header.asp
   ' Author:   Marco Tabini
   ' Date:      February 24, 1998
   ' Revision:   4
%>

<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
   <TITLE><% = Title %> - Web Based DBMS</TITLE>
</HEAD>
<BODY>

<FONT FACE="Verdana">
<TABLE WIDTH=100%>
   <TR>
      <TD COLSPAN=2>
         <FONT SIZE=5>
            <B><% = Title %> - Web Based DBMS</B>
         </FONT>
      </TD>
   </TR>
   <TR>
      <TABLE>
         <TR>
            <TD>
               <FONT SIZE=2>
                  <A HREF="default.asp">Connect</A>
               </FONT>
            </TD>
         <% if Trim (Session ("Connected")) = "True" then %>
            <TD>
               |
            </TD>
            <TD>
               <FONT SIZE=2>
                  <A HREF="Main.ASP">Main</A>
               </FONT>
            </TD>
         <% end if %>
         </TR>
      </TABLE>
   </TR>
   <TR>
      <TD COLSPAN=2>
         <HR>
      </TD>
   </TR>
</TABLE>

<P>&nbsp

<%
   REM -- Error output function

   Sub CheckForError
      If Err.Number <> 0 Then
         Response.Write ("<FONT SIZE=5><B>ERROR</B></FONT>")
         Response.Write ("<P>The following error occurred while performing the requested operation:")
         Response.Write ("<P>" & Err.Number & "&nbsp;" & Err.Description)
         Response.Write ("</BODY></HTML>")
         Response.End
      End If
   End Sub

   REM -- Error-checking wrappers for ADO functions

   Sub SafeMove (NewRow)
      On Error Resume Next
      rs.Move (NewRow)
      CheckForError
   End Sub

   Sub SafeAddNew
      On Error Resume Next
      rs.AddNew
      CheckForError
   End Sub

   Sub SafeUpdate
      On Error Resume Next
      rs.Update
      CheckForError
   End Sub

   Sub SafeMoveNext
      On Error Resume Next
      rs.MoveNext
      CheckForError
   End Sub

   REM -- If login information has been given, open a connection to the database

   if Trim (Session ("Connected")) = "True" then
      On Error Resume Next
      set db = Server.CreateObject ("ADODB.Connection")
      CheckForError
      db.ConnectionString = "Driver={SQL Server};server=" & Session ("Server") & ";DATABASE=" & Session ("Database") & ";UID=" & Session ("UID") & ";PWD=" & Session ("PWD")
      db.Open
      CheckForError

      REM -- Open a recordset if requested by the page

      if Trim (Statement) <> "" then
         set rs = Server.CreateObject ("ADODB.Recordset")
         CheckForError
         rs.Open Statement, db, Recordtype, Locktype
         CheckForError
      end if
   end if
%>

[LISTING TWO]

<%
   ' File:      tablelist.asp
   ' Author:   Marco Tabini
   ' Date:      March 6, 1998
   ' Revision:   2
%>

<%
   ' If no records in table, indicate so

   If rs.RecordCount = 0 Then
%>
      <% if AddEditRecords = True then %>
         <P><A HREF="Record.asp?Operation=NewEdit&Edit=False">Add Record</A>
      <% end if %>
      <P><B>No records in table.</B>
<%
   Else
      ' Browse through the table, one page at a time
      ' The number of records in each page is specified in the 
      ' PageSize variable
%>
      <TABLE>
         <TR>
            <% If Session ("PageNumber") > 0 then %>
            <TD VALIGN=TOP>
               <FORM ACTION="<% = Request.ServerVariables ("SCRIPT_NAME") %>">
                  <INPUT TYPE=HIDDEN NAME="PageNumber" VALUE="<% = Session ("PageNumber") - 1 %>">
                  <INPUT TYPE=HIDDEN NAME="Operation" VALUE="View">
                  <INPUT TYPE=SUBMIT VALUE="<< Previous Page">
               </FORM>
            </TD>
            <% end if %>
            <% if PageSize * (Session ("PageNumber") + 1) - 1 <= Rs.RecordCount then %>
               <TD VALIGN=TOP>
                  <FORM ACTION="<% = Request.ServerVariables ("SCRIPT_NAME") %>">
                     <INPUT TYPE=HIDDEN NAME="PageNumber" VALUE="<% = Session ("PageNumber") + 1 %>">
                     <INPUT TYPE=HIDDEN NAME="Operation" VALUE="View">
                     <INPUT TYPE=SUBMIT VALUE="Next Page >>">
                  </FORM>
               </TD>
            <% end if %>
            <TD VALIGN=TOP>
               Page <% = Session ("PageNumber") + 1 %>
            </TD>
            <% if AddEditRecords = True then %>
               <TD>   
                  <A HREF="Record.asp?Operation=NewEdit&Edit=False">Add Record</A>
               </TD>
            <% end if %>
         </TR>
      </TABLE>

      <TABLE BORDER=0 CELLPADDING=0>
         <TR>
            <TH BGCOLOR="BLACK">
               <FONT COLOR="White">
                  ID #
               </FONT>
            </TH>
      <%
         For Each Field in rs.Fields
      %>
            <TH BGCOLOR="BLACK">
               <FONT COLOR="White">
                  <% = Field.Name %>
               </FONT>
            </TH>
      <%   
         Next
      %>   
         </TR>
      <%
         I = Session ("PageNumber") * PageSize + 1
         SafeMove (I - 1)
         While I mod PageSize <> 0 and not rs.EOF
      %>
         <TR <% If I mod 2 = 0 then %>BGCOLOR=#BFBFFF<% end if %>>
            <TD>
               <A HREF="Record.asp?Operation=View&RowNumber=<% = I - 1 %>"><% = I %></A>&nbsp
            </TD>
      <%
            For Each Field in rs.Fields
      %>   
               <TD>
                  <% = rs (Field.Name) %>&nbsp
               </TD>
      <%
            next
            SafeMovenext
            I = I + 1
         Wend
      %>
         </TR>
      </TABLE>

<% 
   End If 
%>