Query String Parameters are used to pass values from one page to another page. Query String parameters are created by using the ? symbol at the end of the url while navigating to a page. Next to ? symbol, specify the parameter nae followed by = operator followed by the value to pass using that parameter. When you have multiple parameters then separate them with & symbol.
Server.Transfer("~/page2.aspx?pname=value&pname=value")
To read Query String parameters, use query string property of Request object.
Varname=Request.QueryString["pname"]
The following example creates two pages Querystring1.aspx and Querystring2.aspx and withen the 1st page within a gridveiw the details of departments are displayed with select option and when user select a department then the selected deptno will be passed to 2nd page through querystring parameter and within the second page the list of employeees working in that deptnowill be displayed in a gridveiw.
1. Add a page to the website with the name querystring1.aspx, take a gridview on it, set the id of that gridveiw to Gvdept, set its AutoGenerateSelectButton property to true
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApp { public partial class Querystring1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection cn = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true"); SqlCommand cmd = new SqlCommand("Select * from dept", cn); cn.Open(); SqlDataReader dr = cmd.ExecuteReader(); gvdept.DataSource = dr; gvdept.DataBind(); gvdept.DataKeyNames = new string[] { "Deptno" }; cn.Close(); dr.Close(); } protected void gvdept_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { gvdept.SelectedIndex = e.NewSelectedIndex; string Dno = gvdept.SelectedValue.ToString(); Response.Redirect("~/Querystring2.Aspx?Dno=" + Dno); } } }2. Add another page to the website with the name querystring2.aspx. Place a gridveiw on that page, set its id as GvEmp
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace WebApp { public partial class Querystring2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string Dno = Request.QueryString["Dno"].ToString(); string Sql = "Select * from emp where Deptno=" + Dno; SqlConnection cn = new SqlConnection("server=localhost; database=db1;uid=cheryl;trusted_connection=true"); SqlCommand cmd = new SqlCommand(Sql, cn); cn.Open(); SqlDataReader dr = cmd.ExecuteReader(); gvemp.DataSource = dr; gvemp.DataBind(); cn.Close(); dr.Close(); } } }