dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersMigbar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET GridView

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


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Diamond    Points: 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.



 


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)





  • 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 2012Member Level: Diamond   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: 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 2012Member 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">
    <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: 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..


    Feedbacks      

    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.


    Next Resource: How to export Grid View Data to PDF or Excel?
    Previous Resource: Different ways of using Hyperlink control in data controls
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.