Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (26)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
what is the right code to link the asp page t..
by amylisa on 22 Jul 2008 Go To Post

Can Loader.asp Get Form Elements
by azziham on 14 Jul 2008 Go To Post

Good asp resource sites
by codemylife on 3 Jul 2008 Go To Post

Re: Unable to insert data in an Access databa..
by asia on 3 Jul 2008 Go To Post

Re: problem with do while loop
by idsanjeev on 30 Jun 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Generating Random Records from the Database
 

Generating Random Records from the Database
by Faisal Khan.

Introduction
In this article we will learn how to display random records from the database. From the articles so far, you will be able to display records from the database in a variety of ways and do a lot with them. We will add to our ASP-Database knowledge by enabling our ASP scripts to show random records.

Practical Application
This technique can be used in a variety of ASP-Database applications :

  • Ad Management Systems to display random ads from different advertisers.
  • Content Rotator to show random content on the site.

Can also be used in a lot other applications and scripts that I can't think of now. You learn this technique here and use this anyhwere you want.

Access Database
Start Microsoft Access and create a new database. Save it as random.mdb. Now in the 'design view', create a new table and save it as RandomRec. The table should look like following in the 'design view' :

RandomRec - Design View
RandomRec - Design View

Populate the table so that it looks something like this :

RandomRec Table
RandomRec Table

We are now ready to code the ASP page to show these statements randomly.

'rndrec.asp' ASP page
Create a new ASP page and copy the following code into, then save it as rndrec.asp :

<%
	Option Explicit
	Response.Buffer = True
%>
<html>
<head>
	<style>
	p { font-family:verdana; font-size:11px; }
	</style>
</head>
<body>
<br><p align="center">
<%
	' ADO Constant. Dont change this
	Const adCmdText = &H0001

		' Connection string and SQL statement
	Dim query, connStr
		query = "select statement from RandomRec"
		connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
				Server.MapPath("random.mdb")

		' Opening database
	Dim rs
		Set rs = Server.CreateObject("ADODB.Recordset")
		rs.Open query, connStr, 3, , adCmdText

		' Generating random number from total number of records
	Dim intRnd
		Randomize Timer
		intRnd = (Int(RND * rs.RecordCount))

		' Now moving the cursor to random record number
		rs.Move intRnd

		' Showing the random statement
		Response.Write "<b>" & rs("statement") & "</b>"

		' Closing the database
		rs.Close
		Set rs = Nothing
%>
</p>
</body>
</html>

Explanation
You should be able to understand most of the code in rndrec.asp page if you have been following my ASP-Database articles. I will explain the part which involves generating a random number.

	rs.Open query, connStr, 3, , adCmdText

The first thing to remember when generating random records is to set Cursor Type to adOpenKeyset i.e. 3.

	' Generating random number from total number of records
	Dim intRnd
		Randomize Timer
		intRnd = (Int(RND * rs.RecordCount))

Next we declare a variable intRnd to hold the random record number. We use Randomize Timer statement to initialize the random-number generator. Then we use the RND Function to generate a random number between 1 and total number of records availabe ( which we get by using Recordset.RecordCount property ).

	' Now moving the cursor to random record number
		rs.Move intRnd

Once we have got a random number between 1 and total number of records, we can move the Recordset Cursor to that random record by using Recordset.Move Function.

	' Showing the random statement
	Response.Write "<b>" & rs("statement") & "</b>"

Next we show the random record. Once done we close the database connection and remove the Recordset Object that we created.

Running the ASP page
You should place both the random.mdb and rndrec.asp files in the same directory. Assuming that you placed both of them under /rndRec/ directory under your virtual directory, you should use http://127.0.0.1/rndRec/rndrec.asp URL to see your ASP page on your local computer.

You will notice that a random statement is displayed every time you hit the refresh button. Well done! you have successfully created a random statement displayer ASP application.

What have we learned ?
We saw how to show a random record from the database. This technique that you have learned is so very often used that I can't even think of any ASP-Database application that I make which doesn't use it.

All you need to do
Just remember to set the Recordset Cursor location to adOpenKeyset i.e. 3. Then randmize the timer and generate a random number as stated on the last page. After that move the Recordset Cursor to that random record number and show it.

Keep coming back and help make Stardeveloper.com become best online resource for server side languages. Thanks for your time.


 ( No Further Pages )

Download Associated Files
2000082901.zip

Related Articles
  1. How to display records from top 4 database systems using plain ASP?
  2. Uploading Files into an Access Database using plain ASP
  3. Displaying Images from an Access Database using plain ASP
  4. Fastest way of Database Access : Caching Records in Memory
  5. Adding records to the database with ASP
  6. DSN vs DSN less Database Connections
  7. Searching and sorting records in a recordset from the database
  8. Speedup Database Access using GetRows
  9. Inserting Form content into Database with ASP
  10. Using ASP pages to page through Recordsets

Comments/Questions ( Threads: 5, Comments: 5 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Thanks
  2. random records
  3. Related question
  4. Nice file upload example!!
  5. random yearly...

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserverd.