ARTICLE

Getting started with ADO.NET Entity Framework in .NET 3.5 - Part II

Posted by Anand Thakur Articles | ADO.NET in C# October 06, 2008
This article contains a very simple ADO.NET Entity Framework application to update and delete the data to the database using ADO Entity Framework.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
Nevron Chart