Showing posts with label QueryString. Show all posts
Showing posts with label QueryString. Show all posts

Sunday, 27 August 2017

QueryString in Asp.Net

In this post, I will explain about QueryString in Asp.Net.

A QueryString is a collection of characters appended to the end of a page URL. QueryString generally used to pass value from one web page to another web page using URL. A QueryString is easy to use and pass value within web pages. A Query strong can be used to pass a small amount of data, large data cannot be passed using QueryString. QueryString can be attached to URL by using the character "?". We can append single or multiple QueryString parameters to URL. Multiple QueryString parameters can be separated using "&" character.

Advantages:
1.    Easy to use.
2.    Server resources are not required.
3.    All browser supports QueryString.

Disadvantages:
1.    Large data cannot be passed using QueryString.
2.    QueryString are not secure because the parameter value is directly visible to the user.

Example
1. QueryString with a single parameter
www.programmingtrends.com?Param1=Google

1. QueryString with multiple parameters
www.programmingtrends.com?Param1=Google&Param2=India

How to Pass Ampersand (&) as QueryString parameter value in Asp.Net?

In this post, I will explain How to Pass Ampersand (&) as QueryString parameter value in Asp.Net?

In many cases, we need to pass value of variable from one page to another page using QueryString. It generally works fine but when a parameter value contains Ampersand (&) symbol then another page reads wrong parameter value. For example, if suppose Parameter value is "Larsen & Turbo" then another page will read it as "Larsen ".

This happens because generally, Ampersand (&) is used as a separator between 2 parameters in QueryString. You can see the following example,

www.programmingtrends.com?Param1=Google&Param2=India

To solve this problem with An ampersand (&) we can use Server.UrlEncode() function to encode Ampersand (&) in parameter value or we can encode parameter value by replacing '&' with '%26'.

Example:
string Param="Larsen & Turbo";

Response.Redirect("www.programmingtrends.com?param1=" + Server.UrlEncode( Param ));

OR

Response.Redirect("www.programmingtrends.com?param1=" + Param.Replace("&", "%26"));