Grid Edit Update Delete Using Stored Procedures
Grid Edit Update Delete Using Stored Procedures
Screen Design:
DataBase:
create proc Usp_tbl_Dept_Insert ( @DeptName varchar(50) ) as begin insert into tbl_Dept(DeptName)values(@DeptName) end create proc Usp_tbl_Dept_Update ( @DeptId int,
M SUDANARAO PATIBANDLA
@DeptName varchar(50) ) as begin UPDATE tbl_Dept SET DeptName=@DeptName WHERE DeptId=@DeptId End create proc Usp_tbl_Dept_Delete ( @DeptId int ) as begin DELETE FROM tbl_Dept WHERE DeptId=@DeptId End create proc Usp_tbl_Dept_SelectAll as begin select * from tbl_Dept end
M SUDANARAO PATIBANDLA
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Manage_Dept.aspx.cs" Inherits="storedprocedures.Manage_Dept" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> Dept Name : <asp:TextBox ID="txtdept" runat="server"></asp:TextBox> <br /> <asp:Button ID="btnsave" runat="server" Text="Save" onclick="btnsave_Click" /> <br /> <asp:GridView ID="GridDept" runat="server" AutoGenerateColumns="False" DataKeyNames="DeptId" onrowcancelingedit="GridDept_RowCancelingEdit" onrowdeleting="GridDept_RowDeleting" onrowediting="GridDept_RowEditing" onrowupdating="GridDept_RowUpdating"> <Columns> <asp:BoundField DataField="DeptName" HeaderText="Dept Name" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView> </div> </form> </body> </html>
ASPX.CS
using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data; System.Data.SqlClient;
M SUDANARAO PATIBANDLA
private SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]) ; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadGridData(); } } protected void LoadGridData() { con.Open(); SqlCommand cmd = new SqlCommand("Usp_tbl_Dept_SelectAll", con); cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); GridDept.DataSource = ds.Tables[0]; GridDept.DataBind(); con.Close(); } protected void btnsave_Click(object sender, EventArgs e) { try { if (txtdept.Text == "") { Response.Write("please insert the dept name"); } else { con.Open(); SqlCommand cmd = new SqlCommand("Usp_tbl_Dept_Insert", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@DeptName", SqlDbType.VarChar, 50); cmd.Parameters[0].Value = txtdept.Text; cmd.ExecuteNonQuery(); con.Close(); LoadGridData(); } } catch (Exception exp) { Response.Write(exp.Message.ToString()); } }
M SUDANARAO PATIBANDLA
protected void GridDept_RowEditing(object sender, GridViewEditEventArgs e) { GridDept.EditIndex = e.NewEditIndex; LoadGridData(); } protected void GridDept_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridDept.EditIndex = -1; LoadGridData(); } protected void GridDept_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { con.Open(); SqlCommand cmd = new SqlCommand("Usp_tbl_Dept_Update", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@DeptId", SqlDbType.Int, 4); cmd.Parameters[0].Value = int.Parse(GridDept.DataKeys[e.RowIndex].Value.ToString()); TextBox dept_name = (TextBox)GridDept.Rows[e.RowIndex].Cells[0].Controls[0]; cmd.Parameters.Add("@DeptName", SqlDbType.VarChar, 50); cmd.Parameters[1].Value = dept_name.Text; cmd.ExecuteNonQuery(); } catch (Exception exp1) { Response.Write(exp1.Message.ToString()); } con.Close(); GridDept.EditIndex = -1; LoadGridData(); } protected void GridDept_RowDeleting(object sender, GridViewDeleteEventArgs e) { con.Open(); try { string Key = GridDept.DataKeys[e.RowIndex].Value.ToString(); SqlCommand cmd = new SqlCommand("Usp_tbl_Dept_Delete", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@DeptId", SqlDbType.Int, 4, Key);
M SUDANARAO PATIBANDLA
M SUDANARAO PATIBANDLA