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.
7. CODING
in Click event of the add button write this code
In showall button event
You can download the project from Here
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.
7. CODING
in Click event of the add button write this code
using (EFDBEntities efdbectx = new EFDBEntities())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.
{
// Create New Employee Object
empinfo _empifo = new empinfo();
_empifo.empid = int.Parse(txtAddempid.Text);
_empifo.name = txtAddempname.Text;
_empifo.sal = int.Parse(txtAddempsal.Text);
// Add to Memory
efdbectx.AddToempinfoes(_empifo);
// Save to DB
efdbectx.SaveChanges();
}
int empid = Convert.ToInt32(txtEditempid.Text);In the Click event of Update button.
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 = editeinfo1.name.ToString();
txteditempsal.Text = editeinfo1.sal.ToString();
}
int empid = Convert.ToInt32(txtEditempid.Text);In the Delete button event
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
editeinfo2.name = txteditempname.Text;
editeinfo2.sal = int.Parse(txteditempsal.Text);
// save to db
Updateempinfo.SaveChanges();
}
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();
ctx.DeleteObject(_empinfo);
ctx.SaveChanges();
}
In showall button event
using (EFDBEntities ctxAll = new EFDBEntities())
{
var _info = from info in ctxAll.empinfoes select info;
GridView1.DataSource = _info.ToList();
GridView1.DataBind();
}
You can download the project from Here
No comments:
Post a Comment