Thursday, 27 June 2013

How To Update a Grid View Value Using Procedure in ASP .NET

How To Update a Grid View Value Using Procedure in ASP .NET

Step 1: First creating a Stored Procedure using SQL Server. Procedure name as empDetails
CREATE PROCEDURE empDetails @name nchar(10), @id nchar(10), @emp_age nchar(10)
AS
BEGIN
UPDATE 
emp_tbl SETnme = @name,
age = @emp_age

WHERE 
emp_id = @Id
END
Step 2. Design the page. Take a Grid View Control to show data and three textboxes and a button.
<%@ Page Language="C#" AutoEventWireup="true"  
Codevirtual="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" 
namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body><form id="form1" runat="server">
<div><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  BorderStyle="Solid" >  
 <RowStyle BackColor="White" BorderColor="#333333" BorderStyle="Solid" 
 BorderWidth="2px" />
  <Columns><asp:BoundField DataField="nme" HeaderText="Name" />
 <asp:BoundField DataField="age" HeaderText="Age" />
 <asp:BoundField DataField="emp_id" HeaderText="Employe ID" />
  </Columns><HeaderStyle BackColor="#CCCCCC" BorderStyle="Inset" BorderWidth="2px" />
 </asp:GridView>   </div>
   <br /><br /> <table>
    <tr><td>Enter ID(From Grid View):</td><td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
    <tr><td>Enter Name:</td><td>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr>
    <tr><td>Enter Age:</td><td>
 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>
 <tr><td>
<asp:Button ID="Button1" runat="server" Text="Update" onclick="Button1_Click" />
]</td></tr>
    </table>
    </form>
</body>
</html>
Step 3.Default.aspx.cs Code.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
  SqlConnection newConnection = new SqlConnection(@"Data Source=R4R-3EFD13BB468\
 SQLEXPRESS; initial catalog=newDatabase;integrated security=true;");
  protected void Page_Load(object sender, EventArgs e)
  {
    newConnection.Open();
    SqlCommand cmdd = new SqlCommand("Select * from emp_tbl", newConnection);
    SqlDataReader dr = cmdd.ExecuteReader();
    if (dr.HasRows)
    {
       GridView1.DataSource = dr;
       GridView1.DataBind();
    }
    newConnection.Close();
    }
    public void update_value(string emp_name, string Id, string emp_age)
    {
        newConnection.Open();
        SqlCommand cmd = new SqlCommand("empDetails",newConnection);
        cmd.CommandType = CommandType.StoredProcedure; //Procedure
        cmd.Parameters.AddWithValue("@name", emp_name);
        cmd.Parameters.AddWithValue("@id", Id);
        cmd.Parameters.AddWithValue("@emp_age", emp_age);
        cmd.ExecuteNonQuery();
        newConnection.Close();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {  //call the function to updating the grid view
        update_value(TextBox2.Text,TextBox1.Text,TextBox3.Text);   
    }
}

Step 4: Run the Application.
Click the Update Button

No comments: