In my last article "Getting started with ADO.NET Entity Framework in .NET 3.5", we have done select and add operations, now let us extend with update and delete operations.

Now I have added there textboxes fro employee ID, name and title as follows. (you can find it with attached project)





Update:

There is no change in Data layer

In the business layer add the UpdateEmployee method

public string UpdateEmployee(Payroll.Entities.Employee e1)

{

PayrollDAO payrollDAO = new PayrollDAO();

try

{

Employee e2 = null;

e2 = (from emp in payrollDAO.Employee

where emp.EmployeeID == e1.EmployeeID

select emp).First();

e2.Name = e1.Name;

e2.Title = e1.Title;

payrollDAO.SaveChanges();

}

catch

{

return "ERROR";

}

return "SUCCESS";

}

In the Presentation layer, add the following code in update button click

protected void Button2_Click(object sender, EventArgs e)

{

EmployeeService es = new EmployeeService();

DateTime dt = new DateTime(2008, 12, 12);

Payroll.Entities.Employee e1 = Payroll.Entities.Employee.CreateEmployee(Convert.ToInt32(empID.Text), empName.Text, empTitle.Text, dt, "M", "M", dt);

Result.Text = es.UpdateEmployee(e1);

GridView1.DataSource = es.GetEmployee();

GridView1.DataBind();

}

Delete:

There is no change in Data layer

In the business layer add the DeleteEmployee method

public string DeleteEmployee(Payroll.Entities.Employee e1)

{

try

{

PayrollDAO payrollDAO = new PayrollDAO();

Employee e2 = (from emp in payrollDAO.Employee

where emp.EmployeeID == e1.EmployeeID

select emp).First();

payrollDAO.DeleteObject(e2);

payrollDAO.SaveChanges();

}

catch

{

return "ERROR";

}

return "SUCCESS";

}

In the Presentation layer, add the following code in delete button click

protected void Button3_Click(object sender, EventArgs e)

{

EmployeeService es = new EmployeeService();

DateTime dt = new DateTime(2008, 12, 12);

Payroll.Entities.Employee e1 = Payroll.Entities.Employee.CreateEmployee(Convert.ToInt32(empID.Text), "", "", dt, "M", "M", dt);

Result.Text = es.DeleteEmployee(e1);

GridView1.DataSource = es.GetEmployee();

GridView1.DataBind();

}

Now our CRUD operations are over, in next article we will try to include more tables with relationships.

Aah! Another bug! Well, it's the life.