How To Read QueryString In ASP.NET Using C#

Query strings is a method to pass values from one page to another. In this blog, you'll learn:

  1. What is a QueryString?
  2. Why do we use QueryStrings?
  3. How to pass values through Query Strings?
  4. How to retrieve values from Query Strings?

QueryString

A query string is one of the techniques in Web applications to send data from one webform to another through the URL. A query string consists of two parts, field and value, and each of pair separated by ampersand (&).

The ?(question mark in a query string indicates the beginning of a query string and it's value.

There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data.

Query strings are visible to the user, hence should not be used to send sensitive information suck as a username and password, unless encrypted.

To retrieve the query string value, use Request object's QueryString property.

Create a Web Application 

Lets understand with an example, let's create a project in Visual Studio.

  • Launch Visual Studio 
  • Go to File -> New -> Project..., Visual C# , Web. Then select ASP.NET Empty Web Application.
  • Provide the project a name and specify the location.
  • Click OK to create the project.

Add Web Forms

Now we will create two Web Forms.

Right-click on Solution Explorer and add a two web forms, user.aspx and Mas_Employee.aspx.

Send the Values using QueryString

Design your user.aspx as in the following. The page has a Button. 

QueryString In ASP.NET

Copy the following code in your button click event to send multiple query string values (Name, DeptNmae) through URL from user.aspxpage to Mas_Employee.aspx.

protected void btnSend_Click(object sender, EventArgs e)  
{  
   Response.Redirect("/Application /Mas_Employee.aspx?Name=" + txtName.Text + "&DeptName=" + txtDeptName.Text);  
}

Retrieve the values from QueryString

Design your Mas_Employee.aspx as in the following:

QueryString In ASP.NET

To retrieve the QueryString Values (Name, DeptNmae) in Mas_Employee.aspx, copy the following code in your Mas_Employee.aspx.cs.

protected void Page_Load(object sender, EventArgs e)  
{  
   if (!IsPostBack)  
   {  
      if (Request.QueryString["Name"] != null && Request.QueryString["Name"] != string.Empty)  
         lblName.Text = Request.QueryString["Name"];  
  
      if (Request.QueryString["DeptName"] != null && Request.QueryString["Name"] != string.Empty)  
         lblDeptName.Text = Request.QueryString["DeptName"];  
   }  
}

Run the application and Provide Name and DeptName values and then click on Send. At this point you will get the following result.

QueryString In ASP.NET

QueryString In ASP.NET

Conclusion

QueryString are used to pass the data (limited data through URL) from one page to another page . For example. If we want to send one page control values to another page you can use QueryString.

I hope you enjoyed it. Please provide your valuable suggestions and feedback if you found this blog helpful.