insert,update,delete using store procedure in asp.net
Insert Data
view data
Add data in gridview
Edit Data in gridview
Emp.aspx
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" onrowcommand="GridView1_RowCommand"
onrowupdating="GridView1_RowUpdating"
ShowFooter="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
DataKeyNames="emp_no" AutoGenerateColumns="False">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Employee No.">
<ItemTemplate>
<%#Eval("emp_no") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEeno" runat="server" Enabled="false" Text=' <%#Eval("emp_no")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txteno" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEempname" runat="server" Text=' <%#Eval("name")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtempname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<%#Eval("salary") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEsal" runat="server" Text=' <%#Eval("salary")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtsal" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit / Delete">
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/images.jpeg" Width="50" Height="50" CommandName="Edit" />
<asp:ImageButton ID="Image1" runat="server" ImageUrl="~/images2.jpeg" Width="50" Height="50"
CommandName="Delete"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton runat="server" ImageUrl="~/images3.jpeg" Width="50" Height="50" CommandName="Insert" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Panel ID="Panel1" runat="server">
Enter Eno
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
Enter Empname
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
Enter Salary
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Add Record" OnClick="Button1_Click" />
</asp:Panel>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Emp.aspx.cs
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
{
//string con =
@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\proceduredata\App_Data\Database.mdf;Integrated
Security=True;User Instance=True";
//SqlConnection
conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
SqlDataAdapter
da = new SqlDataAdapter();
DataTable
dt = new DataTable();
protected void Page_Load(object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
GridView1.ShowFooter = true;
gd();
}
}
void gd()
{
string
con = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\F-drive\pintu\Program
file\proceduredata\App_Data\Database.mdf;Integrated Security=True;User
Instance=True";
SqlConnection
conn = new SqlConnection(con);
string
q = "select * from emp";
SqlCommand
cmd = new SqlCommand(q,
conn);
conn.Open();
da = new
SqlDataAdapter(cmd);
dt.Clear();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
try
{
if
(dt.Rows.Count == 0)
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
/*DataSet ds =
new DataSet();
string q = "select * from
emp";
SqlDataAdapter sd = new
SqlDataAdapter(q, conn);
sd.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();*/
}
protected void Button1_Click(object
sender, EventArgs e)
{
Insert(TextBox1.Text, TextBox2.Text,
TextBox3.Text);
}
void
Insert(string emp_no, string
name, string salary)
{
string
con = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\F-drive\pintu\Program
file\proceduredata\App_Data\Database.mdf;Integrated Security=True;User
Instance=True";
SqlConnection
conn = new SqlConnection(con);
try
{
SqlCommand
cmd = new SqlCommand("insert_emp", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.AddWithValue("@emp_no",
emp_no);
cmd.Parameters.AddWithValue("@name",
name);
cmd.Parameters.AddWithValue("@salary",
salary);
cmd.ExecuteNonQuery();
conn.Close();
gd();
}
catch(Exception )
{
}
finally
{
conn.Close();
}
}
protected void GridView1_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if
(e.CommandName == "Insert")
{
Label1.Text="hi";
GridViewRow
row = GridView1.FooterRow;
TextBox
texeno = (TextBox)row.FindControl("txteno");
TextBox
texenm = (TextBox)row.FindControl("txtempname");
TextBox
texesal = (TextBox)row.FindControl("txtsal");
Insert(texeno.Text, texenm.Text,
texesal.Text);
gd();
}
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gd();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
GridView1.EditIndex = -1;
gd();
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
string
id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string
con = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\F-drive\pintu\Program
file\proceduredata\App_Data\Database.mdf;Integrated Security=True;User
Instance=True";
SqlConnection
conn = new SqlConnection(con);
try
{
SqlCommand
cmd = new SqlCommand("delete_emp", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.Add("@emp_no",id);
Label1.Text = "hi";
cmd.ExecuteNonQuery();
conn.Close();
gd();
}
catch (Exception)
{
}
finally
{
conn.Close();
}
}
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
//string id =
GridView1.DataKeys[e.RowIndex].Value.ToString();
try
{
string
con = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\F-drive\pintu\Program
file\proceduredata\App_Data\Database.mdf;Integrated Security=True;User
Instance=True";
SqlConnection
conn = new SqlConnection(con);
GridViewRow
row = GridView1.Rows[e.RowIndex];
string
eno = null;
eno =
GridView1.DataKeys[e.RowIndex].Value.ToString();
TextBox
name = (TextBox)row.FindControl("txtEempname");
Label1.Text = name.Text;
TextBox
salary = (TextBox)row.FindControl("txtEsal");
SqlCommand
cmd = new SqlCommand("update_emp", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.Parameters.Add("@emp_no", eno);
cmd.Parameters.Add("@name",name);
cmd.Parameters.Add("@salaty",salary);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
gd();
}
catch
{
}
}
}
0 comments :