Active Server Corner
Quick Site Search
What's Inside
Home
Activity
Books
Excerpts
In-Depth
CodeLibrary
Columns
ActiveTalk
COMSutra
Cornered!
My 3 Cents
Square ONE
Downloads
Events
FAQ's
Jobs
Search
Tools
Product Reviews
Tutorials
Site Info
About Us
Advertising Info
Contact Us
Privacy Policy
Terms of Use
Write for Us
  Square ONE  The game begins here

Passing Values into ASP Pages

by David Berry
Sep 4th, 1999

You might have come across this situation; Let's say you had a catalog page that listed all the products you had to sell on your site. After displaying the page (by retrieving the records from your database using ASP) you wanted the customer to be able to click on a particular product and go to another page to see the details for that product. The purpose of this tutorial is to describe how you pass parameters and values to another ASP page to retrieve details about the item. Just follow the steps below.

Step 1: Create a hyperlink that passes a parameter.

You've probably seen URLs in your browser that have are extremely long with the name of the page and then a ? symbol with a bunch of information after it, such as http://www.xyz.com/register.asp?Name=David.  The question mark indicates that you're passing a parameter within the URL. First let's create a hyperlink to pass values to a details page.

' This is a sample hyperlink that passes a
' QueryString called "Details" with a
' parameter (value) called "Computers".

<a href="details.asp?Details=Computers">Show Details</a>

' Alternately, if you've retrieved the
' catalog from your database using ASP
' you could pass the parameter like this.

<a href="details.asp?Details=<% = RS("ProductType") %>"><% = RS("ProductType") %></a>

In the code above, Details is the name of the QueryString we're going to pass and "Computers" is the value we want to retrieve. Details=Computers is a key-value pair.


Step 2: Retrieving the Values

Once you've created your hyperlink, you can access this information by using the Request object's QueryString collection.  In the details.asp page, you can access the value of the keys by using their names with the Request object.  In this example we're using a System DSN called Catalog and a table called Products.

<%
  Dim Connect ,RS, Query, Details
  Details = Trim(Request("Details"))
  Set Connect = Server.CreateObject("ADODB.Connection")
  Connect.Open "Catalog"

  Set RS = Server.CreateObject("ADODB.Recordset")
  Query = "SELECT * FROM Products "
  Query = Query & "WHERE ProductType='"& Details & "' "
  ' In the above statement the name ProductType,
  ' after the Where clause is the name of the
  ' field in our database that equals the value
  ' of the QueryString that we're passing.
  ' In this case "Computers"

  Query = Query & "ORDER BY [ProductName]"
  RS.Open Query, Connect
%>

Step 3: Checking for empty results

It's always a good idea to check to make sure the the query returned records, if not display an informational message.

<%
  If RS.EOF Then
    Response.Write "There are no Details available for " & Request("Details")"
  Else
  ' Then we would display our information and end the If statement
  End If
%>

Step 4: Displaying our Details

The code for details.asp is as shown below. You can modify it accordingly.

<%@Language=VBScript %>
<HTML>
<BODY>
<%
  Dim Connect ,RS, Query, Details
  ' Get the parameter from the URL
  Details = Trim(Request("Details"))

  ' Open the connection to the database
  Set Connect = Server.CreateObject("ADODB.Connection")
  Connect.Open "Catalog"

  Set RS = Server.CreateObject("ADODB.Recordset")

  ' Create the query to extract records
  Query = "SELECT * FROM Products "
  Query = Query & "WHERE ProductType='"& Details & "' "
  Query = Query & "ORDER BY [ProductName]"
 
  ' Open the recordset
  Set RS = Conn.Execute(Query)

  ' The empty resultset check.

  If RS.EOF Then
    Response.Write "There are no Details available for " & Request("Details")"
  Else
  ' The table to display our results.
%>
<table border="1" width="100%" cellpadding="2">
<tr>
<td><strong><font face="Verdana" size="2">Name</font></strong></td>
<td><strong><font face="Verdana" size="2">Product Type</font></strong></td>
<td><strong><font face="Verdana" size="2">Details</font></strong></td>
<td><strong><font face="Verdana" size="2">Price</font></strong></td>
</tr>
<%
  ' Display all our records until we've
  ' reached the end of the file

  Do While Not RS.EOF
%>
<tr>
<td><font face="Verdana" size="2"><% = RS("Name") %></font></td>
<td><font face="Verdana" size="2"><% = RS("ProductType") %></font></td>
<td><font face="Verdana" size="2"><% = RS("Details") %></font></td>
<td><font face="Verdana" size="2"><% = RS("Price") %></font></td>
</tr>
<%
    ' Move to the next record.
    RS.MoveNext

  ' Keep looping through the Database until
  ' all records have been returned.

  Loop
%>
</table>
<%
  ' Here we end the If statement we started
  ' with our checking for empty resultset

  End If

  ' Close and destroy the RecordSet object
  RS.Close
  Set RS = Nothing

  ' Close and destroy connection object
  Conn.Close
  Set Conn = Nothing
%>
</BODY>
</HTML>

Using the above method you have seen how to pass a value to an ASP page using a QueryString.

About the Author
David Berry is a Microsoft MVP for FrontPage. He runs a FrontPage support site at http://www.net-sites.com/sitebuilder External Site

 






Copyright 1997-2000 Active Server Corner. All rights reserved.

Sponsored by Coastline Web Hosting in Santa Barbara, California