0% found this document useful (0 votes)
124 views

Grid Edit Update Delete Using Stored Procedures

The document describes how to manage a GridView control in ASP.NET using stored procedures. It includes creating stored procedures to insert, update, delete and select data from a database table. The ASPX and ASPX.CS code shows how to call the stored procedures and handle events like adding new records, editing, updating, deleting in the GridView. On page load, it loads data from the database table into the GridView using a select stored procedure. Buttons allow inserting new records which calls an insert stored procedure.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Grid Edit Update Delete Using Stored Procedures

The document describes how to manage a GridView control in ASP.NET using stored procedures. It includes creating stored procedures to insert, update, delete and select data from a database table. The ASPX and ASPX.CS code shows how to call the stored procedures and handle events like adding new records, editing, updating, deleting in the GridView. On page load, it loads data from the database table into the GridView using a select stored procedure. Buttons allow inserting new records which calls an insert stored procedure.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[Managing GridView using StoredProcedures] [2009]

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

[Managing GridView using StoredProcedures] [2009]

@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

[Managing GridView using StoredProcedures] [2009]

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

namespace storedprocedures { public partial class Manage_Dept : System.Web.UI.Page {

M SUDANARAO PATIBANDLA

[Managing GridView using StoredProcedures] [2009]

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

[Managing GridView using StoredProcedures] [2009]

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

[Managing GridView using StoredProcedures] [2009]

cmd.Parameters[0].Value = Key; cmd.ExecuteNonQuery(); } catch (Exception exp) { Response.Write(exp.Message.ToString()); } con.Close(); LoadGridData(); } } }

M SUDANARAO PATIBANDLA

You might also like