HTML and CSS Reference
In-Depth Information
This code checks whether the
CurrentMode
of the
FormView
is
Edit
and, if so, grabs the
BirthDate
and
HireDate
values being bound. The
DataItem
object represents an
Employee
being bound with the
FormView
.
The text boxes showing
BirthDate
and
HireDate
are found using the
FindControl()
method. Finally, the
date values are filled in the text boxes using
yyyy-MM-dd
format.
Writing the CRUD Methods
The web form code-behind contains four methods that perform CRUD operations on the database. These
methods are shown in Listing 5-21.
Listing 5-21.
Methods Performing CRUD Operations
public IQueryable<Employee> GetEmployees()
{
NorthwindEntities db = new NorthwindEntities();
var data = from item in db.Employees
orderby item.EmployeeID
select item;
return data;
}
public void InsertEmployee(Employee e)
{
NorthwindEntities db = new NorthwindEntities();
db.Employees.AddObject(e);
db.SaveChanges();
}
public void UpdateEmployee(Employee e)
{
NorthwindEntities db = new NorthwindEntities();
var data = from item in db.Employees
where item.EmployeeID == e.EmployeeID
select item;
Employee obj = data.SingleOrDefault();
obj.TitleOfCourtesy = e.TitleOfCourtesy;
obj.FirstName = e.FirstName;
obj.LastName = e.LastName;
obj.BirthDate = e.BirthDate;
obj.Title = e.Title;
obj.HireDate = e.HireDate;
obj.Address = e.Address;
obj.City = e.City;
obj.Country = e.Country;
obj.HomePhone = e.HomePhone;
db.SaveChanges();
}