Connecting Tech Pros Worldwide Help | Site Map

Display All Records

By Robert Murdock
Programmer, Darpac Inc.

Show All Records

The follow ASP code displays all the records in a database. It's very simple and easy to create.

It starts out with a database connection, then goes on to querying the database for the wanted records. If the records don't exist it displays an error message if not it will go into a while loop to display all the information. At the very end the database connection is closed.

File: dump.asp

<html>
   
 <head>
 <title>List all.</title>
 </head>
   
 <body BGCOLOR="#ffffff">
 <!--#include file="adovbs.inc"-->
 <%
   
   Set DataConn = Server.CreateObject("ADODB.Connection")
   Set RS = Server.CreateObject("ADODB.RecordSet")
   DataConn.Open "DBQ=" & Server.Mappath("cdemo.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
   
   Set rsDsp = DataConn.Execute("SELECT * FROM tblContact ORDER BY Name")
   
   If rsDsp.EOF Then
   Response.Write "Sorry, no entries in the database!"
   Else
   %>
 <div align="center"><center>
   
 <table BORDER="0" width="700">
   <tr>
     <th width="105">Name</th>
     <th width="105">Email</th>
     <th width="105">Phone</th>
   </tr>
 <%
   While Not rsDsp.EOF
     x = x + 1
     If x = 1 Then
       Response.Write "<TR><TD>" & rsDsp.Fields.Item("Name").Value & "</TD>"
       Response.Write "<TD>" & rsDsp("Email").Value & "</TD>"
       Response.Write "<TD>" & rsDsp("Phone").Value & "</TD></TR>"
     Else 
       Response.Write "<TR><TD BGCOLOR=E4E4E4>" & rsDsp.Fields.Item("Name").Value & "</TD>"
       Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("Email").Value & "</TD>"
       Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("Phone").Value & "</TD></TR>"
       x = 0
     End If
     
     rsDsp.MoveNext
   Wend
   Response.Write "</TABLE>"  
   
   RS.Close
   DataConn.Close
   
   End If
   %>
 </table>
 </center></div>
 </body>
 </html> 

« Updating Records Deleting Records »