Example Of Linq To Insert Upadate and Delete using asp.net with C#
Defualt.aspx
<%@ Page Language="C#"
AutoEventWireup="true" EnableEventValidation="false" 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 bgcolor="black">
<form id="form1" runat="server">
<div>
<table border="1" bordercolor="white">
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="emp_name" ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="emp_address" ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="emp_ph" ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="emp_mo" ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="salary" ForeColor="White"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" BackColor="White" runat="server" Text="add" onclick="Button1_Click" />
<asp:Button ID="Button2" BackColor="White" Visible="false" runat="server" Text="update" onclick="Button2_Click" />
</td>
</tr>
</table>
<%--<asp:LinkButton
ID="LinkButton2" ForeColor="White" runat="server"
onclick="LinkButton2_Click">LinkButton</asp:LinkButton>--%>
<asp:GridView ID="GridView1" DataKeyNames="emp_id" BorderColor="White" ForeColor="White" runat="server"
AutoGenerateColumns="false" onrowediting="GridView1_RowEditing"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<%--<asp:LinkButton
ID="LinkButton1" CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton1" ForeColor="Yellow" CommandName="edit" CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DELETE">
<ItemTemplate>
<%--<asp:LinkButton
ID="LinkButton1" CommandArgument='<%#Eval("emp_id")
%>' runat="server">edit</asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton2" ForeColor="Yellow" CommandName="delete" CommandArgument='<%#Eval("emp_id")
%>' runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_NAME">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("emp_nm")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_add">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("emp_add")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_phone">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("emp_ph")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_mobile">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("emp_mo")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMP_salary">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("emp_salary")
%>'></asp:Label>
</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;
public partial class _Default : System.Web.UI.Page
{
DataClassesDataContext
dc = new DataClassesDataContext();
protected void Page_Load(object
sender, EventArgs e)
{
grid();
}
public void grid()
{
var
emps = dc.pi_emp_datas.Select(kk => kk).ToList();
/*var emps =
from myemp in dc.pi_emp_datas select myemp
{
myemp.emp_id,
myemp.emp_add,
myemp.emp_nm
};*/
GridView1.DataSource = emps;
GridView1.DataBind();
}
protected void Button1_Click(object
sender, EventArgs e)
{
pi_emp_data
dt = new pi_emp_data();
dt.emp_nm = TextBox1.Text;
dt.emp_add = TextBox2.Text;
dt.emp_ph = TextBox3.Text;
dt.emp_mo = TextBox4.Text;
dt.emp_salary = int.Parse(TextBox5.Text);
dc.pi_emp_datas.InsertOnSubmit(dt);
dc.SubmitChanges();
Page_Load(null,
null);
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
//string nm =
null;
//string add
= null;
Session["a"]
= GridView1.DataKeys[e.NewEditIndex].Value.ToString();
Label
l1 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label1");
TextBox1.Text = l1.Text;
Label
l2 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label2");
TextBox2.Text = l2.Text;
Label
l3 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label3");
TextBox3.Text = l3.Text;
Label
l4 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label4");
TextBox4.Text = l4.Text;
Label
l5 = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label5");
TextBox5.Text = l5.Text;
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object
sender, EventArgs e)
{
pi_emp_data
dt = new pi_emp_data();
var
myEmployee = dc.pi_emp_datas.Single(myemp => myemp.emp_id == int.Parse(Session["a"].ToString()));
myEmployee.emp_nm = TextBox1.Text;
myEmployee.emp_add = TextBox2.Text;
myEmployee.emp_ph = TextBox3.Text;
myEmployee.emp_mo = TextBox4.Text;
myEmployee.emp_salary = int.Parse(TextBox5.Text);
try
{
dc.SubmitChanges();
Response.Write("Updated");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Button2.Visible = false;
Button1.Visible = true;
grid();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
/*protected void
LinkButton2_Click(object sender, EventArgs e)
{
pi_emp_data dt = new pi_emp_data();
dt.emp_nm = TextBox1.Text;
dt.emp_add = TextBox2.Text;
dc.pi_emp_datas.InsertOnSubmit(dt);
dc.SubmitChanges();
Page_Load(null, null);
}*/
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Session["b"]
= GridView1.DataKeys[e.RowIndex].Value.ToString();
var
myEmployee = dc.pi_emp_datas.Single(myemp => myemp.emp_id ==int.Parse(Session["b"].ToString()));
dc.pi_emp_datas.DeleteOnSubmit(myEmployee);
try
{
dc.SubmitChanges();
Response.Write("Deleted");
}
catch (Exception
ex)
{
Response.Write(ex.Message);
}
grid();
}
}
0 comments :