Here in this application I will use an Item template, Edit Item template and Footer Template to do CRUD operations.
The following is my Data Table in design mode:
Image 1.
Data in my Data Table:
Image 2.
To create the new application go to Solution Explorer in your application then select Add New Item.
Add an ADO.NET Entity Data Model.
Image 3.
Image 4.
Image 5.
Enter your server information and select your database:
Image 6.
Image 7.
Image 8.
Image 9.
Now open Default.aspx and do the following code:
Now my aspx.cs code is:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace CRUDUsingEntityFramework
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindGrid();
- }
-
- }
-
- void BindGrid()
- {
- using (EmployeeManagementEntities context = new EmployeeManagementEntities())
- {
- if (context.Employee.Count() > 0)
- {
- GVEmployee.DataSource = (from em in context.Employee
- select new { em.Emp_ID, em.Name, em.Designation, em.City, em.Country, em.State }).ToList();
- GVEmployee.DataBind();
- }
- else
- {
- GVEmployee.DataSource = null;
- GVEmployee.DataBind();
- }
- }
- }
-
- protected void GVEmployee_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- GVEmployee.PageIndex = e.NewPageIndex;
- BindGrid();
- }
-
- protected void GVEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "InsertNew")
- {
- GridViewRow row = GVEmployee.FooterRow;
-
- TextBox txtName = row.FindControl("txtEmpNameNew") as TextBox;
- TextBox txtDesignation = row.FindControl("txtDesignationNew") as TextBox;
- TextBox txtCity = row.FindControl("txtCityNew") as TextBox;
- TextBox txtState = row.FindControl("txtStateNew") as TextBox;
- TextBox txtCountry = row.FindControl("txtCountryNew") as TextBox;
-
- using (EmployeeManagementEntities context = new EmployeeManagementEntities())
- {
- Employee obj = new Employee();
- obj.Name = txtName.Text;
- obj.Designation = txtDesignation.Text;
- obj.City = txtCity.Text;
- obj.State = txtState.Text;
- obj.Country = txtCountry.Text;
- context.Employee.Add(obj);
- context.SaveChanges();
- BindGrid();
- }
-
- }
- }
- protected void GVEmployee_RowEditing(object sender, GridViewEditEventArgs e)
- {
- GVEmployee.EditIndex = e.NewEditIndex;
- BindGrid();
- }
- protected void GVEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- GVEmployee.EditIndex = -1;
- BindGrid();
- }
- protected void GVEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- GridViewRow row = GVEmployee.Rows[e.RowIndex];
-
- TextBox txtName = row.FindControl("txtEmpName") as TextBox;
- TextBox txtDesignation = row.FindControl("txtDesignation") as TextBox;
- TextBox txtCity = row.FindControl("txtCity") as TextBox;
- TextBox txtState = row.FindControl("txtState") as TextBox;
- TextBox txtCountry = row.FindControl("txtCountry") as TextBox;
-
-
- using (EmployeeManagementEntities context = new EmployeeManagementEntities())
- {
- int employeeID = Convert.ToInt32(GVEmployee.DataKeys[e.RowIndex].Value);
- Employee obj = context.Employee.First(x => x.Emp_ID == employeeID);
- obj.Name = txtName.Text;
- obj.Designation = txtDesignation.Text;
- obj.City = txtCity.Text;
- obj.State = txtState.Text;
- obj.Country = txtCountry.Text;
- context.SaveChanges();
-
- GVEmployee.EditIndex = -1;
- BindGrid();
- }
-
- }
-
- protected void GVEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- int employeeID = Convert.ToInt32(GVEmployee.DataKeys[e.RowIndex].Value);
- using (EmployeeManagementEntities context = new EmployeeManagementEntities())
- {
- Employee obj = context.Employee.First(x => x.Emp_ID == employeeID);
- context.Employee.Remove(obj);
- context.SaveChanges();
- BindGrid();
- }
- }
- }
- }
Now run your application: All Record
Image 10.
Add a new record:
Image 11.
Go to Page 2 as I have Page Size 10 here.
Image 12.
Now edit any record:
Image 13.
Image 14.
Now delete any record:
Image 15.