ASP.NET Technology

How to use QueryString

Assume we need to pass certain Variable content between html or aspx pages, often the Request object of QueryString is used. QueryStrings are combination of {key=value} pairs joined by ‘&’ and added to the html or aspx page Url. This blog will explain in simple terms how to use QueryString.


If your page is Default.aspx and you need to pass the NameValue combination, name=myname, then the QueryString would be Default.aspx?name=myname.

Now our problem is to pass variables for the categories
category1=object1
category2=object2
category2=object3

So we are having 1 value for category1 and 2 values for category2;

In your aspx page, add the following Namespaces,

using System.Web;
using System.Collections.Specialized;

Now from the Key-Value pair in QueryString, to obtain all the keys, we use the following code,

NameValueCollection coll = Request.QueryString;
String[] arr1 = coll.AllKeys;

Now to get the Values for Keys,

String[] arr2 = coll.GetValues(arr1[0]); //Values for category1
String[] arr3 = coll.GetValues(arr1[1]); //Values for category2

It is evident that in arr2 you will have only 1 value, and arr3 will have 2 values.

Advantages of using QueryString

  1. It is very easy.

Disadvantages of using QueryString

  1. QueryString have a max length, If you have to send a lot information this approach does not work.
  2. QueryString is visible in your address part of your browser so you should not use it with sensitive information.
  3. QueryString can not be used to send & and space characters.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *