SIGN UP MEMBER LOGIN:    
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
share this article :
post comment
 

I am new but I think Its a better way for developer.

Posted by MA Bari Feb 27, 2011

good

Posted by Krishna Reddy Apr 12, 2010

ADO.NET Entity Framework is included with .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1. Please make sure you have and Visual Studio 2008 Service Pack 1 installed in your system.

thanks,
Mohamed Taraman

Posted by Mohamed Said Taraman Nov 28, 2009

I am trying to create a new test project i dont get the ADO.NET Enttity Frame option in VS 2008 though i have .NET 3.5 with Service Pack 1.

Posted by baktash ahmed May 08, 2009

I can not compile this project for few errors.

Posted by ihtesham qazi May 03, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor