Step-1:- Add class as Employee .
Step-2:- Add Reference - > Assemblies - > Select
System.Data.Linq -> Ok .
Step-3:- Make a new “Data Connection” with database server. View -> Server Explorer -> Data Connections -> Add Connection.
Step-4:- Add Connection String in Web.Config .
Employee.cs:-
using System.Data.Linq.Mapping;
[Table(Name = "tblEmployee")]
public class Employee
{
[Column(Name = "pkEmpId", IsPrimaryKey = true, IsDbGenerated = true)]
public int EmpId
{
get;
set;
}
[Column]
public string EmpName
{
get;
set;
}
[Column]
public decimal EmpSalary
{
get;
set;
}
public string EmpDoj
{
get;
set;
}
}
CRUD Operation
Design
CRUD OPERATION
Curd_Operation.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.Configuration;
using System.Data.Linq;
public partial class frmTest : System.Web.UI.Page
{
DataContext dc = null;
public frmTest()
{
dc = new DataContext(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
}
protected void ShowEmp()
{
try
{
Table<Employee> tbEmp = dc.GetTable<Employee>();
GridView1.DataSource = tbEmp;
GridView1.DataBind();
}
catch (Exception)
{
Response.Write("internal issue");
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
ShowEmp();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
Employee empObj = new Employee();
empObj.EmpName = txtEmpName.Text;
empObj.EmpSalary = Convert.ToInt64(txtEmpSalary.Text);
empObj.EmpDoj = txtEmpDoj.Text;
dc.GetTable<Employee>().InsertOnSubmit(empObj);
dc.SubmitChanges();
ShowEmp();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Table<Employee> tbEmp = dc.GetTable<Employee>();
Employee empObj = (from m in tbEmp where m.EmpId == Convert.ToInt32(txtEmpId.Text) select m).FirstOrDefault();
empObj.EmpName = txtEmpName.Text;
empObj.EmpSalary = Convert.ToInt64(txtEmpSalary.Text);
empObj.EmpDoj = txtEmpDoj.Text;
dc.SubmitChanges();
ShowEmp();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Table<Employee> tbEmp = dc.GetTable<Employee>();
Employee empObj = (from m in tbEmp where m.EmpId == Convert.ToInt32(txtEmpId.Text) select m).FirstOrDefault();
dc.GetTable<Employee>().DeleteOnSubmit(empObj);
dc.SubmitChanges();
ShowEmp();
}
}
No comments:
Post a Comment