How to show selected record from grid view to another page?
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.
Description:
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");
return;
}
sqlcmd = new SqlCommand("select * from stud where sid='" + sid + "'", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = sid;
TextBox2.Text = dt.Rows[0][1].ToString();
TextBox3.Text = dt.Rows[0][2].ToString();
}
sqlcon.Close();
}
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#
Conclusion:
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.
How do i extract the single value of a gridview and display in the different page?