Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Wednesday 14 May
MembersPro PayPal - ASP Membership software

asp.netPRO “Best ASP.NET Host” – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Displaying the top 10 records

The code below will simply show the top 10 records from our "tblFriends" table in the database called 'Friends.mdb'. The table has 3 columns, firstly an 'ID' field that is an autonumber, secondly a field called 'FirstName' which is a textfield and lastly another textfield called 'SurName'.

Call our file 'display_records.asp'

<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Displaying all records from a database table</title>
</head>
<body>
<%
'declare your variables
Dim Connection, Recordset
Dim sSQL, sConnString

'declare SQL statement that will query the database
sSQL="SELECT TOP 10 ID, FirstName, SurName FROM tblFriends"

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")


'create an ADO connection and recordset
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
connection.Open sConnString

'Open the recordset object, execute the SQL statement
recordset.Open sSQL,connection

'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else
'If there are records then loop through the fields
Do While Not recordset.EOF
Response.Write recordset("ID") & " " & recordset("FirstName") & " " & _
recordset("SurName")
Response.Write "<br>"  'insert a line break
'move on to the next record
Recordset.MoveNext
Loop
End If

'close the connection and recordset objects and free up resources
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
%>
</body>
</html>

You can change the number of records you want to retrieve and display by simply changing the number.
If there are no entries then the message 'No records returned.' will be displayed.

Previous: SORTING Records
LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software