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.


Attachments

  • Grid_View_Linkbutton_queryString (42819-15234-Gridview_linkButton.rar)
  • Comments

    Guest Author: samreen21 Sep 2012

    How do i extract the single value of a gridview and display in the different page?

    Author: Ravindran25 Sep 2012 Member Level: Gold   Points : 1

    samreen,

    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

    http://www.dotnetspider.com/resources/43119-How-get-Grid-view-control-cell-value.aspx

    Guest Author: pranjal22 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: Ravindran29 Oct 2012 Member Level: Gold   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">
    <Columns>
    <asp:BoundField DataField="eno" HeaderText="Employee No." />

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

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



    .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
    }
    }


    Page2.aspx.cs


    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"];
    sqlcon.Open();
    sqlcmd = new SqlCommand("select * from emp where eno='" + eno + "'", sqlcon);
    da = new SqlDataAdapter(sqlcmd);
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
    txteno.Text=dt.Rows[0]["eno"].ToString();
    txtempname.Text=dt.Rows[0]["empname"].ToString();
    txtsal.Text=dt.Rows[0]["sal"].ToString();
    }
    }

    }

    }

    Guest Author: Sonali02 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: amitesh14 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: Sagar08 Apr 2013

    Thanks for your great article.....

    Author: naresh10 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: naveensanagasetti10 Dec 2013 Member Level: Gold   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.



  • 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:
    Email: