Resources » Code Snippets » ASP.NET GridView

How to show selected record from grid view to another page?

Updated: Category: ASP.NET GridView
Author: Member Level: DiamondPoints: 7

In this article I have explained about how to show selected record details from grid view into another page one page. In the second page we update the selected Record details. This article I used to transfer a primary key value using Query String option.

Grid view is used to display records from database some time we need to edit that data instead of grid view edit option we want to send that details another page and update that record values from that values. So I used the Link Button in the grid view for show user Identity Primary key value if user click that link button I send that value as query string to another page.

How to send selected record ID field as query string?
Here I used GridView1_RowCommand event for send user selected record identity value.I check that user command event use of "e.CommandName" if match command value then redirect to another page.
Below code is send value through query string

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "cmdBind")
LinkButton lb = (LinkButton)e.CommandSource;

Response.Redirect("Default2.aspx?sid=" + lb.Text + "");


How to get query string value ?
You can get query string value in another page (redirected page) Page_load event.

if (!Page.IsPostBack)
String sid = Request.QueryString["sid"];
if (sid == "")
Response.Write("Invalid Student ID");
sqlcmd = new SqlCommand("select * from stud where sid='" + sid + "'", sqlcon);
da = new SqlDataAdapter(sqlcmd);
if (dt.Rows.Count > 0)
TextBox1.Text = sid;
TextBox2.Text = dt.Rows[0][1].ToString();
TextBox3.Text = dt.Rows[0][2].ToString();

Source Code Detail:
Here with I have attached entire source code. Download it and try to work on query string operation in Grid View
Front End : ASP.NET
Code Behind : C#

I hope this Article is help to you for how to send query string value from grid view and get query string value in another page.

Did you like this resource? Share it with your friends and show your love!

Responses to "How to show selected record from grid view to another page?"
Guest Author: samreen   21 Sep 2012
How do i extract the single value of a gridview and display in the different page?

Author: Ravindran  25 Sep 2012   Member Level: Diamond   Points : 1

You can get cell value using my below resource code and stored in session value or pass as query string to display in other page

Guest Author: pranjal   22 Oct 2012
hello sir/madam

i m working on project where in gridview i have imagebutton when i clic on imagebtn another age is opened where all details of book is shown.pls help i need code

Author: Ravindran  29 Oct 2012   Member Level: Diamond   Points : 5
.aspx page
Refer below sample code

<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="false" onpageindexchanging="GridView1_PageIndexChanging" PageSize="5"
AllowPaging="true" onrowcommand="GridView1_RowCommand">
<asp:BoundField DataField="eno" HeaderText="Employee No." />

<asp:BoundField DataField="empname" HeaderText="Employee Name" />
<asp:BoundField DataField="sal" HeaderText="Salary" />

<asp:TemplateField HeaderText="Details">
<asp:ImageButton ID="Image1" runat="Server" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" CausesValidation="false" ImageUrl="~/images/p1.png" />

.aspx.cs page

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "cmdBind")
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
string pkey;
//Bind values in the text box of the pop up control
pkey = GridView1.Rows[index].Cells[0].Text;
Response.Redirect("Page2.aspx?eno=" + pkey + "); //here i have transfer primary key second page taken that query string in second page and get data for that particular record and update it


using System.Data;
using System.Data.SqlClient;

public partial class GridHypher : System.Web.UI.Page
SqlConnection sqlcon = new SqlConnection(@"Server=SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;");
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();

protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
string eno;
eno = Request.QueryString["eno"];
sqlcmd = new SqlCommand("select * from emp where eno='" + eno + "'", sqlcon);
da = new SqlDataAdapter(sqlcmd);
if (dt.Rows.Count > 0)



Guest Author: Sonali   02 Feb 2013
can you tell me please how the following will possible using linq to sql
i have database table with user id ,name and address, i have connected database to gridview and create name has templete field and display name on linkbutton in grid view and i want that when i clicked that button i want all information of that user to be displayed on label

Guest Author: amitesh   14 Mar 2013
In this code i can't edit sid if i want to edit sid , how can i edit sid in it..

Guest Author: Sagar   08 Apr 2013
Thanks for your great article.....

Author: naresh  10 Dec 2013   Member Level: Bronze   Points : 2
my requirement is select an item in gridview and navigate to another page with use of QueryString where i can get the complete costmer details from the db using query string user id

Author: naveensanagasetti  10 Dec 2013   Member Level: Diamond   Points : 4
No. of ways we can do this. In that one of the best way is use HyperLinkField template of gridview and use DataNavigateUrlFields property to pass parameters from one page to another page.

Use below sample to achieve our goal

asp:HyperLinkField Text="View" DataNavigateUrlFields="CL_Id,CL_Desc,CL_Short_Desc" DataNavigateUrlFormatString="~/_Layouts/DMS/ChecklistItems.aspx?CL_Id={0}&CL_Desc={1}&CL_Short_Desc={2}"

and in page2, you just call that parameters using QueryString.

string CL_Id="",CL_Desc="",CL_Short_Desc="";

if( Request.QueryString["CL_Id"] != null)
CL_Id = Request.QueryString["CL_Id"].ToString();

if( Request.QueryString["CL_Desc"] != null)
CL_Desc= Request.QueryString["CL_Desc"].ToString();

if( Request.QueryString["CL_Short_Desc"] != null)
CL_Short_Desc= Request.QueryString["CL_Short_Desc"].ToString();

we can able to pass the values like above.

i think the best and easy way to pass Gridview Parameters to another page using HyperLinkField.

Hope this will help you someone else those who are looking for the same.

Author: laxman  10 Nov 2014   Member Level: Bronze   Points : 2
my requirement is select an item in gridview and navigate to another page with use of QueryString when a user checks checkbox and click submit button the specified user data should be displayed in next page. How to write code for this


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India