Using the ASP QueryString

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

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:


<a href=”″>File 8</a>

Once the user clicked on the link, they would of course be directed to To get the querystring id, you would do the following:


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.


<!–#include virtual=”” –>
Dim DB, RS
Set DB = Server.CreateObject(“adodb.connection”)
DB.Mode = adModeReadWrite
DB.ConnectionString = Server.MapPath(“db1.mdb”)
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>”

Response.Write RS(“file_description”)

End If


Set RS = Nothing


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:


<a href=”″>File 1 </a> <BR>

<a href=”″>File 2 </a> <BR>

<a href=”″>File 3 </a> <BR>

<a href=”″>File 4 </a> <BR>

<a href=”″>File 5 </a> <BR>

<a href=”″>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:

To retrieve these values, do the following:



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:


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


Author: Andrew Schools