How To Read QueryString In ASP.NET Using C#

Learn how to use a query string in ASP.NET to pass and retrieve values from one page to another 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. 
 
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.
  1. protected void btnSend_Click(object sender, EventArgs e)  
  2. {  
  3.    Response.Redirect("/Application /Mas_Employee.aspx?Name=" + txtName.Text + "&DeptName=" + txtDeptName.Text);  
  4. }  
Retrieve the values from QueryString:
 
Design your Mas_Employee.aspx as in the following:
 
 
To retrieve the QueryString Values (Name, DeptNmae) in Mas_Employee.aspx, copy the following code in your Mas_Employee.aspx.cs.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    if (!IsPostBack)  
  4.    {  
  5.       if (Request.QueryString["Name"] != null && Request.QueryString["Name"] != string.Empty)  
  6.          lblName.Text = Request.QueryString["Name"];  
  7.   
  8.       if (Request.QueryString["DeptName"] != null && Request.QueryString["Name"] != string.Empty)  
  9.          lblDeptName.Text = Request.QueryString["DeptName"];  
  10.    }  
  11. }  
Run the application and Provide Name and DeptName values and then click on Send. At this point you will get the following result.
 
 
 
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.