Thursday, May 31, 2012

Insert, Update and Detele in Entity Framework using C#.Net

Hi this post is regarding EntityFramework where we can can do simple queries like Select, Edit, Update and Delete using EF (EntityFramework). EF is an Object-Relational Mapper.

     First create a database, Don't worry i am uploading the database file also just attach the db file and use it. 
How to attach DB can be seen HERE

1. Open VS 2010 and create a new web project.
2. Ones the web project was created, the first thing we need to do is ADD a new item from installed templates      in vs 2010. Here the new item is ADO.Net Entity Model with an extension *.edmx.
     In my project EFDB  is my file name. ones it was added EFDBEENTITY is the class name to create
     an object, to work with it.
3. while adding a new item it will ask for a connection, provide appropriate things and add the tables you need.
4. Now build the project ones.
5. Design the GUI for adding deleting updating, editing and selecting, now i didnt had a chance to show the
    screen shots for u. Download the project from the provided link.
6. After designing the GUI we had 5 buttons and 7 text boxes and a gridview to show the details.

in Click event of the add button write this code

            using (EFDBEntities efdbectx = new EFDBEntities())
                // Create New Employee Object
                empinfo _empifo = new empinfo();
                _empifo.empid = int.Parse(txtAddempid.Text);
       = txtAddempname.Text;
                _empifo.sal = int.Parse(txtAddempsal.Text);

                // Add to Memory

                // Save to DB
In the Click event of  (EDIT) Update button. In this event you need to provide the employee id to fetch the data and bind to the  text boxes. We call this type of binding as lazy binding.

            int empid = Convert.ToInt32(txtEditempid.Text);
            using (EFDBEntities efdbctx = new EFDBEntities())
                // Get a specified emp from db
                empinfo editeinfo1 = (from _empinfo in efdbctx.empinfoes where
               _empinfo.empid == empid  select _empinfo).First();

                //Bind to Controls
                txtEditempid.Text = editeinfo1.empid.ToString();
                txteditempname.Text =;
                txteditempsal.Text = editeinfo1.sal.ToString();
 In the Click event of Update button.

            int empid = Convert.ToInt32(txtEditempid.Text);
            using (EFDBEntities Updateempinfo = new EFDBEntities())
                // Get a specified emp from db
                empinfo editeinfo2 = (from _empinfo in Updateempinfo.empinfoes where
                _empinfo.empid == empid select _empinfo).First();

                // changes goes here
       = txteditempname.Text;
                editeinfo2.sal = int.Parse(txteditempsal.Text);

                // save to db
In the Delete button event

            int Delempid = Convert.ToInt32(txtdelempno.Text);
            using (EFDBEntities ctx = new EFDBEntities())
                empinfo _empinfo = (from e1 in ctx.empinfoes where e1.empid == Delempid select e1).First();

In showall button event

            using (EFDBEntities ctxAll = new EFDBEntities())
                var _info = from info in ctxAll.empinfoes select info;
                GridView1.DataSource = _info.ToList();

You can download the project from Here

No comments:

Post a Comment