Example Of Insert update delete data using interface in asp.net with C#
Default.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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="emp_name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="emp_add"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="emp_salary"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button2" runat="server" Text="Veiw" onclick="Button2_Click" />
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="submit" onclick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button3" runat="server" Text="Update" onclick="Button3_Click" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" DataKeyNames="emp_id" runat="server"
onrowediting="GridView1_RowEditing" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%#Eval("emp_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("emp_name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("emp_add")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="emp_salary">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#Eval("emp_salary")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="edit" >Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete" >delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
emp_interfaceDataContext
emp = new emp_interfaceDataContext();
intr2 i2
= new intr2();
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
grid();
}
}
protected void Button1_Click(object
sender, EventArgs e)
{
string
nm = String.IsNullOrEmpty(TextBox1.Text) ? null : TextBox1.Text;
string
ad = String.IsNullOrEmpty(TextBox2.Text) ? null : TextBox2.Text;
string
s = String.IsNullOrEmpty(TextBox3.Text) ? "0" : TextBox3.Text;
int a =
int.Parse(s);
//int sk =
Convert.ToInt32(TextBox3.Text) > 0 ?(int?)Convert.ToInt32(TextBox3.Text) :
null;
//fk_departmentid
= dk > 0 ? (int?)dk:null;
i2.insertemp(nm, ad, a);
grid();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
/*if
(s==null)
{
i2.insertemp(nm, ad, int.Parse(s));
}
else
{
i2.insertemp(nm, ad,
int.Parse(TextBox3.Text));
}*/
//int sa = sa
= (TextBox3.Text))>0 ? sa : null;
//TextBox3.Text
= Convert.ToInt32(TextBox3.Text) > 0
? TextBox3.Text : null;
//i2.insertemp(nm,
ad, Convert.ToInt32(TextBox3.Text));
//int k = sa
< 0 ? (int?)null : sa;
// string a="null";
//sa =
(int.Parse(TextBox3.Text));
// sa =
int.Parse(a.ToString());
//int sal =
int.TryParse(TextBox3.Text,out sa) ? sa : (int?)null;
}
protected void Button2_Click(object
sender, EventArgs e)
{
//DataTable
d = new DataTable();
//d =
i2.view();
grid();
}
public void grid()
{
GridView1.DataSource = i2.view();
GridView1.DataBind();
TextBox1.Text = "";
TextBox2.Text = ""; TextBox3.Text = "";
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
Session["id"]
= GridView1.DataKeys[e.NewEditIndex].Value.ToString();
Label
enm = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label4");
TextBox1.Text = enm.Text;
Label
eadd = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label5");
TextBox2.Text = eadd.Text;
Label
esal = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label6");
TextBox3.Text = esal.Text;
}
protected void Button3_Click(object
sender, EventArgs e)
{
i2.updatemp(Convert.ToInt32(Session["id"].ToString()), TextBox1.Text,
TextBox2.Text,Convert.ToInt32(TextBox3.Text));
grid();
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Session["delete"]
= GridView1.DataKeys[e.RowIndex].Value.ToString();
i2.deleteemp(Convert.ToInt32(Session["delete"].ToString()));
grid();
}
}
0 comments :