I decided to add a second answer based on the correct comments from Ahmed. Session variables really should not contain the amount of gridview data due to memory issues.
The following should work accordingly for what I assume you are doing:
In fact, when you select a row to go to the next page, you are trying to get the data of this row on a new page. Is this Assumption right? If so, you have several use cases.
Again, you can use session variables to store the data of the string extracted on the first page:
protected void btnformatric_Click(object sender, EventArgs e) { if (gvDoctorList.SelectedRow != null) { GridViewRow selectedRow = gvDoctorList.SelectedRow; Session["PatientId"] = selectedRow.Cells[0].Text; Session["firstname"] = selectedRow.Cells[1].Text; Session["lastname"] = selectedRow.Cells[2].Text; Server.Transfer("Patientstaticformatrix.aspx"); } else { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true); } }
Essentially here you are on the first page and get the data from the row. You then store this data in session variables, and you can use the following data to find data from the following page:
protected void Page_Load(object sender, EventArgs e) { if (this.Page.PreviousPage != null) { //Retrieve values from Session Variables Response.Write("PatientId: " + Session["PatientId"].ToString() + "<br />"); Response.Write("firstname: " + Session["firstname"].ToString() + "<br />"); Response.Write("lastname: " + Session["lastname"].ToString() + "<br />"); } }
You also have a second option for using Query Strings to transfer data. Although for this method I believe you will have to change Server.Transfer("Patientstaticformatrix.aspx"); as Response.Redirect("Patientstaticformatrix.aspx");
The following is an example of using query strings:
protected void btnformatric_Click(object sender, EventArgs e) { if (gvDoctorList.SelectedRow != null) { GridViewRow selectedRow = gvDoctorList.SelectedRow; //Create URL with Query strings to redirect to new page Response.Redirect("Patientstaticformatrix.aspx?parentid=" + selectedRow.Cells[0].Text + "&firstname=" + selectedRow.Cells[1].Text + "&lastname=" + selectedRow.Cells[2].Text); } else { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a row.')", true); } }
And get the values ββfrom the Request.QueryString object on the second page.
protected void Page_Load(object sender, EventArgs e) { if (this.Page.PreviousPage != null) { //Retrieve values from Query Strings Response.Write("PatientId: " + Request.QueryString["parentid"].ToString() + "<br />"); Response.Write("firstname: " + Request.QueryString["firstname"].ToString() + "<br />"); Response.Write("lastname: " + Request.QueryString["lastname"].ToString() + "<br />"); } }
Both of these solutions should meet your requirements, but they are both slightly different. The Session Variable solution is probably the preferred method, because it will not allow users to see all the transmitted data (if you need to transmit confidential information), where, since the values ββof the query string will be available to anyone who can see the URL,
For more information about session variables and query strings, see the following resources:
ASP.NET Session Status Overview
Request.QueryString Collection