Tutorial: Database - ASP - Display Security Users With Access Database
 

Dr. Thomas E. Hicks
Computer Science Department
Trinity University

 


Tutorials

 In order to use ODBC to enable web pages to update and query a database, the host computer must be running web server software. One of the Tutorials below might help you install web server software on your computer.

 IIS 5  Install on Windows XP Pro

IIS 5 Installation on Windows 2000 Pro/Server

Installing PWS on Windows 98

Install Apache With IIS

It might even help to read a tutorial that describes the need for Database Web Applications.

Database Need For Web Applications

You may use the basic ideas of this tutorial for either the ODBC connection or the MapPath connection..

Database - ASP - ODBC Connections

Database - ASP - MapPath Connections
 


Access Database - Security.dbc

For purposes of discussion, let us suppose that an Access Database, called Security.mdb, resides at the root of drive C. (See Below!) When a database is installed on the web server, the necessary ODBC drivers are also installed. If the web server does not have the database installed, then the ODBC drivers will have to be downloaded and installed; this process differs from database to database and is beyond the scope of this paper. Microsoft Access and the appropriate ODBC drivers have been installed on the server illustrated below.

The Security Database is opened and the Tables tab is selected; in order to keep our example simplistic, the Security Database has only a single table, called Users. The contents of our very simple Users table can be seen below.

Each record in the Users table contains fields Name, No, Password, and IDNo; these will be needed later.

The datatypes of each field may be seen below.

The Security Database above was is ultra trivial application which had only one database table. Relational databases often have many tables; this is true of Access databases as well. This database may be downloaded.

Security.zip


Relative Path To The Security.dbc

We are going to place all of the ASP files in folder C:\Inetpub\wwwroot\ASP.  We could place the database files anywhere on the drive. For security reasons, it is not a good idea to place them in the wwwroot directory.

For purposes of this tutorial, we shall assume that the Security database files are located in C:\Security. For purposes of this tutorial, we shall assume that the Security database is located in C:\.


The Code

 
<%@ LANGUAGE = VBScript %>

The server will have one default scripting language. This may or may not be VBScript. This line makes sure that the scripting language is VBScript. Blocks of ASP code begin with <% and end with %>.


<% Option Explicit %>  

This line of ASP code forces the programmer to explicitly declare each and every variable.


<% Response.Expires = 0 %>

This line of ASP reloads the most recent database data each time the page is refreshed.


<!--  #include virtual = "../../Common/adovbs.inc"  --> 

Variables, such as AdOpenDynamic and AdLockOptimistic are defined in the adovbs.inc file. I keep a copy of this file in folder C:\Inetpub\WWWRoot\Common.


<%
'========================================================================
'========================================================================
'====                    DisplaySecurityUsers.asp                    ====
'========================================================================
'====                                                                ====
'==== Purpose    : Demonstrate the MapPath connection to the         ====
'====              Security database located in folder Security that ====
'====              is three levels up at C:\                         ====
'====                                                                ====
'==== Written By : Dr. Thomas E. Hicks               Date: 6/1/2003  ====
'========================================================================
'========================================================================

'------------------------------------------------------------------------
'                              Declarations
'------------------------------------------------------------------------
Dim Conn, UserSQL, UserRecordSet, ConnString, Counter

The documentation block provides a brief statement of purpose. Immediately following the documentation block are declarations for all of the variables used on this page.


'------------------------------------------------------------------------
'                             Initializations
'------------------------------------------------------------------------
Counter = 1

Counter is initialized to 1. It shall be used to number the rows/records in the table.


'------------------------------------------------------------------------
'                    Establish ODBC Connection To The Server
'------------------------------------------------------------------------
'Create a Connection Object
Set Conn = Server.CreateObject("ADODB.Connection")

ConnString = "SourceType=DBC; SourceDB=" &_
             Server.MapPath("../../../Security/Security.dbc")
Conn.Open "Driver={Microsoft Visual FoxPro Driver}; " & ConnString

The block of code above connects this page to the Security database. A connection object is created. Variable ConnString contains part of the connection arguments. The Conn.Open opens the Security database and associates it with the connection object.


'------------------------------------------------------------------------
'                        Read All Of The User Information
'------------------------------------------------------------------------

UserSQL = " Select * " &_
              " From User"

Set UserRecordSet = Server.CreateObject ("ADODB.RecordSet")
UserRecordSet.Open UserSQL, Conn, AdOpenDynamic, AdLockOptimistic
%>

The SQL query is to select all of the information from the User table of the connected Security database. A UserRecordSet  object is created.

When the record set object is passed the SQL query and the connection object, all of the information about all of the Users is transferred from the server to your record set.


<html><head>
<title>Dr. Thomas E. Hicks - DisplaySecurityUsers.asp </title>
</head>

The HTML places Dr. Thomas E. Hicks - DisplaySecurityUsers.asp in the browser title bar.


<BODY 	TEXT      = "#000000" 
	BGCOLOR   = "#000000" 
	VLINK      ="#000000" 
	ALINK      ="#000000" 
	BACKGROUND ="Paper.jpg">

This HTML code above defines the page background, the default text color, the default background color, and the default link colors.


<CENTER>
<font face="Arial" size="4">DisplaySecurityUsers.asp<br>
Written By<br>
Dr. Thomas E. Hicks</font></p>
</CENTER>
<hr>

The HTML code above creates the following commercial at the top of the page.

DisplaySecurityUsers.asp
Written By
Dr. Thomas E. Hicks


<!-- ================================================================ -->
<!--                      Display Users In The Table              -->
<!-- ================================================================ -->
<font face="System" size="3" color="#FFFFFF">
<TABLE 	BORDER      = "5" 
	CELLPADDING = "4" 
	CELLSPACING = "4" 
	STYLE       = "border-collapse: collapse" 
	BORDERCOLOR = "#800000" 
	BGCOLOR     = "#FFFFFF"
	WIDTH       = "100%" 
	ID          = "UserTable">

Although it is not essential, we are going to display the information from the database in a traditional HTML table. The code above establishes the format for this table.


<!-- ================================================================ -->
<!--                            Title Bar Row                         -->
<!-- ================================================================ -->
<TR><TD ALIGN = "Right"> 
<CENTER> <font color="#000080">#</font></CENTER></TD>
<TD ALIGN = "Left"> <font color="#000080">Name</font></TD>
<TD ALIGN = "Right"> 
<CENTER> <font color="#000080">No</font></CENTER></TD>
<TD ALIGN = "Left"> <font color="#000080">Phone</font></TD>
</TR>

The HTML code above creates the following Table Title Bar in the first row of the table.

#

Name

No

Phone

<%
	Do While NOT UserRecordSet.EOF
%>

If the query is successful, the record set will contain information returned from the server. The record set pointer initially points to the first record in the record set. This record set point can be moved through the record set.

We would like to display the information from the current record in a row of our table and then move this record set pointer to the next record. This process shall continue in a Do While loop until the end of file is reached.

The basic form for the ASP Do While Loop is :
    Do While [Condition]
           ...........
    Loop


<!-- ================================================================ -->
<!--                          One Row Per Record                      -->
<!-- ================================================================ -->
<TR>
<TD ALIGN = "Center"> <% = Counter %>                          </TD>
<TD ALIGN = "Left">   <% =UserRecordSet.Fields("Name") %>  </TD>
<TD ALIGN = "Center"> <% =UserRecordSet.Fields("No") %>    </TD>
<TD ALIGN = "Left">   <% =UserRecordSet.Fields("Phone") %> </TD>
</TR>

All of the code from the block above dumps output into a single row of the HTML table. A Counter is displayed in the first column. The current record's Name is placed in the second column.  The current record's No is placed in the third column.  The current record's Phone is placed in the fourth column.


<%
     Counter = Counter + 1
     UserRecordSet.MoveNext
     Loop 
%>

Counter is simply a counter to number the records; it is incremented each pass through the loop.
The record set pointer is moved to the next record.

Loop transfers control back to the Do While statement and repeats the block of code within the loop untill the condition is no longer true.  This loop terminates when the record set pointer reaches the end of file.


</TABLE>
</BODY></HTML>

The HTML  code above simply ends the table, the body, and the document.


Complete Code For DisplaySecurityUsers.asp

The complete code may be found below. A working model may be found at

DisplaySecurityUsers.aspasp

Although it is not the purpose of this tutorial to teach ASP or HTML, a example is necessary to verify that the connection works.

<%@ LANGUAGE = VBScript %>
<% Option Explicit %>  
<% Response.Expires = 0 %>
<!--  #include virtual = "../../Common/adovbs.inc"  --> 

<%
'========================================================================
'========================================================================
'====                   DisplaySecurityUsers.asp                     ====
'========================================================================
'====                                                                ====
'==== Purpose    : Demonstrate the MapPath connection to the         ====
'====              Security database located in folder Security that ====
'====              is three levels up at C:\                         ====
'====                                                                ====
'==== Written By : Dr. Thomas E. Hicks               Date: 6/1/2003  ====
'========================================================================
'========================================================================

'------------------------------------------------------------------------
'                              Declarations
'------------------------------------------------------------------------
Dim Conn, UserSQL, UserRecordSet, ConnString, Counter

'------------------------------------------------------------------------
'                             Initializations
'------------------------------------------------------------------------
Counter = 1

'------------------------------------------------------------------------
'      Create The Console Object & MapPath Connection To The Server
'------------------------------------------------------------------------
'Create a Connection Object
Set Conn = Server.CreateObject("ADODB.Connection")

ConnString = "SourceType=DBC; SourceDB=" &_
             Server.MapPath("../../../Security/Security.dbc")
Conn.Open "Driver={Microsoft Visual FoxPro Driver}; " & ConnString

'------------------------------------------------------------------------
'                        Read All Of The User Information
'------------------------------------------------------------------------

UserSQL = " Select *   " &_
          " From Users "

Set UserRecordSet = Server.CreateObject ("ADODB.RecordSet")
UserRecordSet.Open UserSQL, Conn, AdOpenDynamic, AdLockOptimistic

%>

<html><head>
<title>Dr. Thomas E. Hicks - DisplaySecurityUsers.asp </title>
</head>

<BODY 	TEXT      = "#000000" 
	BGCOLOR   = "#000000" 
	VLINK      ="#000000" 
	ALINK      ="#000000" 
	BACKGROUND ="Paper.jpg">

<CENTER>
<font face="Arial" size="4">DisplaySecurityUsers.asp<br>
Written By<br>
Dr. Thomas E. Hicks</font></p>
</CENTER>
<hr>

<!-- ================================================================ -->
<!--                      Display Users In The Table              -->
<!-- ================================================================ -->
<font face="System" size="3" color="#FFFFFF">
<TABLE 	BORDER      = "5" 
	CELLPADDING = "4" 
	CELLSPACING = "4" 
	STYLE       = "border-collapse: collapse" 
	BORDERCOLOR = "#800000" 
	BGCOLOR     = "#FFFFFF"
	WIDTH       = "100%" 
	ID          = "UserTable">

<!-- ================================================================ -->
<!--                            Title Bar Row                         -->
<!-- ================================================================ -->
<TR><TD ALIGN = "Right"> 
<CENTER> <font color="#000080">#</font></CENTER></TD>
<TD ALIGN = "Left"> <font color="#000080">Name</font></TD>
<TD ALIGN = "Right"> 
<CENTER> <font color="#000080">No</font></CENTER></TD>
<TD ALIGN = "Left"> <font color="#000080">Phone</font></TD>
</TR>

<%
     Do While NOT UserRecordSet.EOF
%>
<!-- ================================================================ -->
<!--                          One Row Per Record                      -->
<!-- ================================================================ -->
<TR>
<TD ALIGN = "Center"> <% = Counter %>&nbsp;</TD>
<TD ALIGN = "Left"> <% =UserRecordSet.Fields("Name") %>&nbsp;</TD>
<TD ALIGN = "Center"> <% =UserRecordSet.Fields("No") %> &nbsp;</TD>
<TD ALIGN = "Left"> <% =UserRecordSet.Fields("Phone") %> &nbsp;</TD>
</TR>

<%
	Counter = Counter + 1
	UserRecordSet.MoveNext
	Loop 
%>
</TABLE>
</BODY></HTML>

You can see the results below:


Other Tutorials

Internet Database Tutorials

Database Need For Web Applications

ASP - ODBC Connections

ASP - MapPath Connections

ASP - Display Security Users With Access Database

 ASP - Display Video Customers With FoxPro Database

 ASP - Display Video Movies With FoxPro Database

ASP - Display Video Transact With FoxPro Database

ASP - Display Video Relationships With FoxPro Database

ASP - Display Video Who Checked Out What With FoxPro Database

ASP - Search Security For Users By Name With Access Database

ASP - Search Security For Users By No With Access Database
ASP - Delete Security For Users By Name With Access Database
ASP - Delete Security For Users By No With Access Database
ASP - Add Security Users With Access Database
 

IDC-HTX-ODBC Database Applications

Database Form Guidelines


May be accessed through URL: http://www.cs.trinity.edu/~thicks
May also be accessed through URL: http://carme.cs.trinity.edu
This Document May Not Be Printed or Reproduced Without Written Permission.
 2003 Copyright : Dr. Thomas E. Hicks
Permission granted : Professional Educators & College Students may print one copy of this page!

Dr. Thomas E. Hicks

Computer Science Department    
Trinity University

"Dr. Web"