Send GridView Row Values to Another Page and Update Record Values

This article explains how to send GridView row values to another page and update the record values and return to the original GridView page using ASP.NET.

1.
Go to Start, then All Programs and open Microsoft Visual Studio 2013.
2. Now, click "File" -> "New" -> "Project..." then click on Visual C#. Then select ASP.NET Web Application, Empty and click on OK.
3. Provide the web application name and location as you wish. I named my web application UpdateGridviewRowValuesInSecondPage.
4. Now the project will be opened. Right-click on the web application name, add a New Item and select Web Form. Then click on Add.
5. Add the following code between the <form> tags of the WebForm1.aspx page as in the following:
  1. <div>  
  2.             <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">  
  3.                 <Columns>  
  4.                     <asp:BoundField DataField="EmpNo" HeaderText="EmpId" />  
  5.                     <asp:BoundField DataField="EmpName" HeaderText="EmpName" />  
  6.                     <asp:BoundField DataField="Job" HeaderText="Job" />  
  7.                     <asp:BoundField DataField="Sal" HeaderText="EmpSalary" />  
  8.                     <asp:BoundField DataField="Dept" HeaderText="Department" />  
  9.                     <asp:TemplateField>  
  10.                         <ItemTemplate>  
  11.                             <asp:Button ID="btnEdit" runat="server" Width="60" Text="Edit" CommandName="EditButton" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />  
  12.                         </ItemTemplate>  
  13.                     </asp:TemplateField>  
  14.                 </Columns>  
  15.             </asp:GridView>  
  16.         </div>  
6. Add the following code to the page load event of the WebForm1.aspx.cs page.
  1. protected void Page_Load(object sender, EventArgs e)    
  2. {    
  3.     if (!this.IsPostBack)    
  4.     {    
  5.         BindGridView();    
  6.     }                
  7. }    
  8.   
  9. private void BindGridView()    
  10. {    
  11.     string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;    
  12.     SqlConnection con = new SqlConnection(constr);    
  13.     SqlCommand cmd = new SqlCommand("select * from Emp",con);    
  14.     DataTable dt = new DataTable();    
  15.     SqlDataAdapter da = new SqlDataAdapter(cmd);    
  16.     da.Fill(dt);    
  17.     GridView1.DataSource = dt;    
  18.     GridView1.DataBind();    
  19. }  
7. The following is the GridView1_RowCommand event method of the WebForm1.aspx.cs page.
  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)    
  2. {    
  3.     if (e.CommandName == "EditButton")    
  4.     {    
  5.         int index = Convert.ToInt32(e.CommandArgument);    
  6.         GridViewRow row = GridView1.Rows[index];    
  7.         Response.Redirect("~/UpdateGridView.aspx?EmpNo=" + row.Cells[0].Text);    
  8.     }    
  9. }  
8. Add another webform to the project and name it as you wish. I named it UpdateGridView.aspx.
9. Add the following code between the <form> tags of the UpdateGridView.aspx page.
  1. <div>    
  2.    <table>    
  3.        <tr>    
  4.            <td>EmpId</td>    
  5.            <td>    
  6.                <asp:TextBox ID="txtEmpId" runat="server" ReadOnly="true" />    
  7.            </td>    
  8.        </tr>    
  9.        <tr>    
  10.            <td>EmpName</td>    
  11.            <td>    
  12.                <asp:TextBox ID="txtEmpName" runat="server" />    
  13.            </td>    
  14.        </tr>    
  15.        <tr>    
  16.            <td>Job</td>    
  17.            <td>    
  18.                <asp:TextBox ID="txtJob" runat="server" />    
  19.            </td>    
  20.        </tr>    
  21.        <tr>    
  22.            <td>EmpSalary</td>    
  23.            <td>    
  24.                <asp:TextBox ID="txtEmpSalary" runat="server" />    
  25.            </td>    
  26.        </tr>    
  27.        <tr>    
  28.            <td>Department</td>    
  29.            <td>    
  30.                <asp:TextBox ID="txtDept" runat="server" />    
  31.            </td>    
  32.        </tr>    
  33.        <tr>    
  34.            <td align="center">    
  35.                 <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />    
  36.            </td>    
  37.            <td align="center">    
  38.                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />    
  39.            </td>    
  40.        </tr>    
  41.    </table>    
  42. lt;/div>    
10. The following is the page load event of the UpdateGridView.aspx.cs page.
  1. int empno = 0;    
  2. protected void Page_Load(object sender, EventArgs e)    
  3. {    
  4.     empno = Convert.ToInt32(Request.QueryString["EmpNo"].ToString());    
  5.     if (!IsPostBack)    
  6.     {    
  7.         BindTextBoxvalues();    
  8.     }    
  9. }    
  10.   
  11. private void BindTextBoxvalues()    
  12. {    
  13.     string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;    
  14.     SqlConnection con = new SqlConnection(constr);    
  15.     SqlCommand cmd = new SqlCommand("select * from Emp where EmpNo=" + empno, con);    
  16.     DataTable dt = new DataTable();    
  17.     SqlDataAdapter da = new SqlDataAdapter(cmd);    
  18.     da.Fill(dt);    
  19.     txtEmpId.Text = dt.Rows[0][0].ToString();    
  20.     txtEmpName.Text = dt.Rows[0][1].ToString();    
  21.     txtJob.Text = dt.Rows[0][2].ToString();    
  22.     txtEmpSalary.Text = dt.Rows[0][3].ToString();    
  23.     txtDept.Text = dt.Rows[0][4].ToString();    
  24. }   
11. The following is the btnUpdate_Click event method of the UpdateGridView.aspx page.
  1. protected void btnUpdate_Click(object sender, EventArgs e)    
  2. {    
  3.     string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;    
  4.     SqlConnection con = new SqlConnection(constr);    
  5.     SqlCommand cmd = new SqlCommand("update Emp set EmpName='" + txtEmpName.Text + "',Job='" + txtJob.Text + "',Sal=" + txtEmpSalary.Text + ",Dept='" + txtDept.Text + "' where EmpNo=" + empno, con);    
  6.     con.Open();    
  7.     int result = cmd.ExecuteNonQuery();    
  8.     con.Close();    
  9.     if (result == 1)    
  10.     {    
  11.         ScriptManager.RegisterStartupScript(thisthis.GetType(), "ShowSuccess""javascript:alert('Record Updated Successfully');"true);    
  12.     }    
  13.     Response.Redirect("~/WebForm1.aspx");    
  14. }  
12. The following is the btnCancel_Click event method of the UpdateGridView.aspx page.
  1. protected void btnCancel_Click(object sender, EventArgs e)  
  2. {  
  3.     Response.Redirect("~/WebForm1.aspx");  
  4. }  
13. Now run the application and you can see it in your browser.
 
  
14. The following shows updating the employee salary of the EmpId 4 record from 39000 to 49000.
 
  
15. After the update, check the GridView page for whether the update is done or not.
 
 


Similar Articles