Using the ASP QueryString

What’s the QueryStringWhen you visit a site, you may sometimes see a url like this:

http://www.codehungry.com/index.asp?id=12345&name=codehungry

What’s all this? Well, before the ?, it’s just a plain old url, however, everything after the ? is considered the QueryString.

So what’s so special about the QueryString? It’s very powerful and of course, very useful. For instance, if you wanted to get a certain file from a database, you would give the user a link to click:

content.asp

<a href=”http://www.codehungry.com/index.asp?id=8″>File 8</a>

Once the user clicked on the link, they would of course be directed to http://www.codehungry.com/index.asp?id=8. To get the querystring id, you would do the following:

Request.QueryString(“id”)

If you wanted to write the value of the querystring id, you would do the following:

Response.Write Request.QueryString(“id”)

Which would give you a value of 8.

Now in order to make the QueryString useful, we need to create a script that will read through the database looking for the id that’s = 8. If you do not understand this, then you should read the tutorial Simple Database Techniques.

index.asp?id=8

<!–#include virtual=”adovbs.inc” –>
<%
Dim DB, RS
Set DB = Server.CreateObject(“adodb.connection”)
DB.Mode = adModeReadWrite
DB.Provider = “MICROSOFT.JET.OLEDB.4.0”
DB.ConnectionString = Server.MapPath(“db1.mdb”)
DB.Open
Set RS = Server.CreateObject(“adoDB.recordset”)
RS.Open “SELECT * FROM table1 WHERE id= ” & Request.QueryString(“id”), DB, adOpenKeySet,adLockOptimistic,adCmdText

If RS.BOF and RS.EOF Then

Response.Write “<p align=’center’>Sorry, no files!</p>”

Else
Response.Write RS(“file_description”)

End If

RS.Close

Set RS = Nothing

DB.Close

Set DB = Nothing
%>

The following code will obtain a file from the database with it’s id = 8.

So, if you wanted to create an content page, create a list of links like so:

content.asp

<a href=”http://www.codehungry.com/index.asp?id=1″>File 1 </a> <BR>

<a href=”http://www.codehungry.com/index.asp?id=2″>File 2 </a> <BR>

<a href=”http://www.codehungry.com/index.asp?id=3″>File 3 </a> <BR>

<a href=”http://www.codehungry.com/index.asp?id=4″>File 4 </a> <BR>

<a href=”http://www.codehungry.com/index.asp?id=5″>File 5 </a> <BR>

<a href=”http://www.codehungry.com/index.asp?id=6″>File 6 </a> <BR>

When the user clicks on one of the links, they will be taken to index.asp, which is a processing page that processes the QueryString. It then takes the value of the QueryString id and finds the correct file in the database.

Using more than one QueryString variable

What if you want to use more than one QueryString variable in your QueryString? You need to separate them by a &. For instance:

http://www.codehungry.com/index.asp?id=8&name=codehungry

To retrieve these values, do the following:

Request.QueryString(“id”)

Request.QueryString(“name”)

When using spaces in your QueryString or special characters like the ‘ character, you must use the URLEncode method of the Server object:

text = “Andrew’s WebSite!”

Response.Write “<a href=’index.asp?id=” & Server.UrlEncode(text) & “‘>Click Here!</a>”

You should get the following QueryString:

index.asp?id=Andrew%27s+WebSite%21

To retrieve this sentence from the QueryString, you would do what you always have:

Request.QueryString(“id”)

Author: Andrew Schools