Skip to main content

ASP.NET QueryString Structure, Example and Limitations

Several time in ASP.NET applications we need to transfer data or information provided by user from one aspx page to another. To transfer data we can use several method, QueryString is one of them.
Query strings are data that is appended to the end of a page URL. They are commonly used to hold data like page numbers or search terms or other data that isn't confidential. Unlike ViewState and hidden fields, the user can see the values which the query string holds without using special operations like View Source.
QueryString is an example of GET method. QueryString is very handy when we need to pass low secure small content from one page to another page. But if we need to pass secure information like credit card number etc or large amount of data then we can't use QueryString.

Structure of Query String
Query strings are appended to the end of a URL. First a question mark is appended to the URL's end and then every parameter that we want to hold in the query string. The parameters declare the parameter name followed by = symbol which followed by the data to hold. Every parameter is separated with the ampersand symbol.
You should always use the HttpUtility.UrlEncode method on the data itself before appending it.

Here are example of how we can use QueryString across pages.

QueryString example [C#]

Pass the QueryString to another page as:

Here we have passed fname as QueryString to Default.aspx page. Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:

    public partial class _Default : Page
        protected void Page_Load(object sender, EventArgs e)
            if (Request.QueryString["fname"] != null)
                String firstName = Request.QueryString["fname"];
                Response.Write("First Name is " + firstName);
    First Name is Sandeep

Two query string parameters

Now we have familiar with QueryString, Here we see how you can test two query string URL parameters. Another common requirement is to test two different query strings. You may have to use either one or both at once. 

Pass the two QueryString as:

In above line we pass two querystring with &, we can pass more querystring like this if we required.

Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:

  public partial class _Default : Page
        protected void Page_Load(object sender, EventArgs e)
            if (Request.QueryString["fname"] != null &&           Request.QueryString["lname"] != null)
                String firstName = Request.QueryString["fname"];
                String lastName = Request.QueryString["lname"];
                Response.Write("Name is " + firstName + " " + lastName);

    Name is Sandeep Tada
Query String Limitations
You can use query string technique when passing from one page to another but that is all. If the first page need to pass non secure data(as mentioned above) to the other page it can build a URL with a query string and then redirect. You should always keep in mind that a query string isn't secure and therefore always validate the data you received. There are a few browser limitation when using query strings. For example, there are browsers that impose a length limitation on the query string. 
Another limitation is that query strings are passed only in HTTP GET command. 
Maximum length of Querystring in ASP.NET
Maximum length of Querystring is based on the browser not depend upon the here is some information

Maximum length of a querystring in IE 4.0 and above is ~2048 characters  

IE. 4,5,6,7, - 2,048 characters. 

Opera supports - 4050 characters. 

Netscape 6 supports - 2000 characters. 

Firefox Supports - 6000 characters  

Cheers!!!! हैप्पी प्रोग्रम्मिंग :)

Popular posts from this blog

C#: Merging Excel cells with NPOI HSSFWorkbook

In this post we’ll see how to merge the two or more cell with each other while creating the excel sheet using NPOI.
Mentioned below is code to merge multiple cells, in this example we are merging first cell to fifth cell of first row (you can adjust row or cell range by passing particular parameters in CellRangeAddress).
//Created new Workbook var hwb = new NPOI.HSSF.UserModel.HSSFWorkbook(); //Create worksheet with name. var sheet = hwb.CreateSheet("new sheet"); //Create row and cell. var row = sheet.CreateRow(0); var cell = row.CreateCell(0); ; //Set text inside cell cell.SetCellValue("This is Merged cell"); cell.CellStyle.WrapText = true; //define cell range address // parameters: -> first row to last and first cell to last cell var cra = new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 4); //Add merged region to sheet. sheet.AddMergedRegion(cra);
Hope this solution helps you J

Read and parse a CSV file into an array of rows and columns in C#

The following PopulateCsvIntoArraymethod used to read the CSV file into a two-dimensional array of strings.
I have included explanation of method lines, wherever needed. ///<summary> /// Populate the CSV file into an array, /// We assume that every line has the same number of fields and there may be blank lines. ///</summary> ///<returns></returns> privatestring[,] PopulateCsvIntoArray() { // Get path of CSV file. var path = Server.MapPath("~/Folder_Name/testfile.csv"); // Get the file's text using ReadAllText method. string fileData = System.IO.File.ReadAllText(path);
// Split CSV data into lines.     fileData = fileData.Replace('\n', '\r'); string[] lines = fileData.Split(newchar[] { '\r' }, StringSplitOptions.RemoveEmptyEntries);
// Get rows and columns counts. int totalRows = lines.Length;

Calculate Sum and average of two or more Textbox values using JQuery

We can calculate sum and average of values entered in textboxes with the help of small piece of jQuery code.
First of all let’s create a HTML form where we’ll ask user to enter marks in each subject, under body section add following HTML:
<tableclass="demo-table"> <trclass="label"> <tdcolspan="2">Please enter marks(0-99):</td> </tr> <tr> <td>English</td> <td> <inputtype="text"class="txtMarks"name="txtMarks"