How to create CRUD using Linq to Entity in ASP.NET 4?

In this project, I use Microsoft Visual C# 2010 Express Edition and Microsoft Visual Web Developer 2010 Express Edition

You need to create a class library project first. Then create a ADO.NET Entity Framework, then create a class and interface for each table or object.

Step 1 – Find and Click Start Then Find and Click Microsoft Visual C# 2010 Express

Step 2 – Find and Click New Project under File.

Step 3 – Find and Select Class Library then enter your chosen name for this class library project.

Step 4 – Click on Add new data Source under data.

Step 5 – Right Click and select Add Then new item.

Step 6 – Select ADO.NET Entity Data Model then type in your chosen name for your entity model.

Step 7 – Choose “Generate from Database” then click next.

Step 8 – Click on “New Connection …” to create new connection. Then type in your chosen name for your connection string. Then click on next.

Step 9 – Choose your objects Then type in your chosen name for your model namespace.

Step 10 – All your chosen objects will be generated automatically.

To create a class for each tables, here are the steps:

Steps 1 – Right Click and select Add Then New Item. You need to add class.

Step 2 – Choose Class template, then type in your chosen class name.

Step 3 – To create interface, just repeat the steps in creating a class.

Here are the samples codes for both interface and class file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MyLinqToEntityClassLibrary

{

    interface PersonInterface

    {

        List<tblPerson> RetrievePersonList();

        List<tblPerson> RetrievePersonListByName(string name, string sortExpression, string sortDirection);

        tblPerson RetrievePersonByID(int id);

        tblPerson RetrievePersonByName(string name);

        int CreatePerson(tblPerson person);

        int UpdatePerson(tblPerson person);

        int DeletePerson(int id);

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

namespace MyLinqToEntityClassLibrary

{

    public class PersonClassificationCompanyCityCountryPosition

    {

        tblPerson person;

        tblClassification classification;

        tblCompany company;

        tblCity city;

        tblCountry country;

        tblPosition position;

        string personName = string.Empty;

        public PersonClassificationCompanyCityCountryPosition()

        {

            person = new tblPerson();

            classification = new tblClassification();

            company = new tblCompany();

            city = new tblCity();

            country = new tblCountry();

            position = new tblPosition();

        }

        public DateTime PersonBirthDate

        {

            get { return person.PersonBirthDate; }

            set

            {

                if (person.PersonBirthDate != value)

                    person.PersonBirthDate = value;

            }

        }

        public int ClassificationId

        {

            get { return person.PersonClassificationId; }

            set

            {

                if (person.PersonClassificationId != value)

                    person.PersonClassificationId = value;

            }

        }

        public string ClassificationName

        {

            get { return classification.ClassificationName; }

            set

            {

                if (classification.ClassificationName != value)

                    classification.ClassificationName = value;

            }

        }

        public int? CompanyId

        {

            get { return person.PersonCompanyId; }

            set

            {

                if (person.PersonCompanyId != value)

                    person.PersonCompanyId = value;

            }

        }

        public string CompanyName

        {

            get { return company.CompanyName; }

            set

            {

                if (company.CompanyName != value)

                    company.CompanyName = value;

            }

        }

        public string PersonFirstName

        {

            get { return person.PersonFirstName; }

            set

            {

                if (person.PersonFirstName != value)

                    person.PersonFirstName = value;

            }

        }

        public string PersonGender

        {

            get { return person.PersonGender; }

            set

            {

                if (person.PersonGender != value)

                    person.PersonGender = value;

            }

        }

        public string PersonHomeAddress

        {

            get { return person.PersonHomeAddress; }

            set

            {

                if (person.PersonHomeAddress != value)

                    person.PersonHomeAddress = value;

            }

        }

        public string PersonHomeCellularPhone

        {

            get { return person.PersonHomeCellularPhone; }

            set

            {

                if (person.PersonHomeCellularPhone != value)

                    person.PersonHomeCellularPhone = value;

            }

        }

        public int CityId

        {

            get { return person.PersonHomeCityId; }

            set

            {

                if (person.PersonHomeCityId != value)

                    person.PersonHomeCityId = value;

            }

        }

        public string CityName

        {

            get { return city.CityName; }

            set

            {

                if (city.CityName != value)

                    city.CityName = value;

            }

        }

        public int CountryId

        {

            get { return person.PersonHomeCountryId; }

            set

            {

                if (person.PersonHomeCountryId != value)

                    person.PersonHomeCountryId = value;

            }

        }

        public string CountryName

        {

            get { return country.CountryName; }

            set

            {

                if (country.CountryName != value)

                    country.CountryName = value;

            }

        }

        public string PersonHomeEmail

        {

            get { return person.PersonHomeEmail; }

            set

            {

                if (person.PersonHomeEmail != value)

                    person.PersonHomeEmail = value;

            }

        }

        public string PersonHomePhone

        {

            get { return person.PersonHomePhone; }

            set

            {

                if (person.PersonHomePhone != value)

                    person.PersonHomePhone = value;

            }

        }

        public int PersonID

        {

            get { return person.PersonId; }

            set

            {

                if (person.PersonId != value)

                    person.PersonId = value;

            }

        }

        public string PersonLastName

        {

            get { return person.PersonLastName; }

            set

            {

                if (person.PersonLastName != value)

                    person.PersonLastName = value;

            }

        }

        public string PersonMiddleName

        {

            get { return person.PersonMiddleName; }

            set

            {

                if (person.PersonMiddleName != value)

                    person.PersonMiddleName = value;

            }

        }

        public int? PositionId

        {

            get { return person.PersonPositionId; }

            set

            {

                if (person.PersonPositionId != value)

                    person.PersonPositionId = value;

            }

        }

        public string PositionName

        {

            get { return position.PositionName; }

            set

            {

                if (position.PositionName != value)

                    position.PositionName = value;

            }

        }

        public string PersonName

        {

            get

            {

                if (personName == null | personName == string.Empty)

                    personName = person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName;

                return personName;

            }

            set

            {

                if (personName != value)

                    personName = value;

                if (personName == null | personName == string.Empty)

                    personName = person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName;

            }

        }

    }

    public class PersonClassificationCompanyCityCountryPositionBLL

    {

        PersonClassificationCompanyCityCountryPosition personClassificationCompanyCityCountryPosition;

        List<PersonClassificationCompanyCityCountryPosition> personClassificationCompanyCityCountryPositionList;

        TestDBEntitiesConnectionString dbEntity;

        string connString = ConfigurationManager.ConnectionStrings[“TestDBEntitiesConnectionString”].ConnectionString;

        IOrderedQueryable personQuery;

        public PersonClassificationCompanyCityCountryPositionBLL()

        {

            personClassificationCompanyCityCountryPosition = new PersonClassificationCompanyCityCountryPosition();

            personClassificationCompanyCityCountryPositionList = new List<PersonClassificationCompanyCityCountryPosition>();

            dbEntity = new TestDBEntitiesConnectionString(connString);

        }

        public List<PersonClassificationCompanyCityCountryPosition> RetrievePersonNameListByName(string name, string sortExpression, string sortDirection)

        {

            List<PersonClassificationCompanyCityCountryPosition> list = new List<PersonClassificationCompanyCityCountryPosition>();

            List<tblPerson> personList = new List<tblPerson>();

            List<tblCity> cityList = new List<tblCity>();

            List<tblClassification> classificationList = new List<tblClassification>();

            List<tblCompany> companyList = new List<tblCompany>();

            List<tblCountry> countryList = new List<tblCountry>();

            List<tblPosition> positionList = new List<tblPosition>();

            PersonClass personBll = new PersonClass();

            CityClass cityBll = new CityClass();

            CountryClass countryBll = new CountryClass();

            CompanyClass companyBll = new CompanyClass();

            PositionClass positionBll = new PositionClass();

            ClassificationClass classificationBll = new ClassificationClass();

            cityList = cityBll.RetrieveCityList();

            countryList = countryBll.RetrieveCountryList();

            companyList = companyBll.RetrieveCompanyList();

            classificationList = classificationBll.RetrieveClassificationList();

            positionList = positionBll.RetrievePositionList();

            personList = (from p in dbEntity.tblPersons

                          where (p.PersonFirstName + ” ” + p.PersonMiddleName + ” ” + p.PersonLastName).Contains(name)

                          select p).ToList<tblPerson>();

            foreach (tblPerson person in personList)

            {

                personClassificationCompanyCityCountryPosition = new PersonClassificationCompanyCityCountryPosition();

                personClassificationCompanyCityCountryPosition.CityId = person.PersonHomeCityId;

                foreach (tblCity city in cityList)

                {

                    if (city.CityId == person.PersonHomeCityId)

                    {

                        personClassificationCompanyCityCountryPosition.CityName = city.CityName;

                        break;

                    }

                }

                personClassificationCompanyCityCountryPosition.ClassificationId = person.PersonClassificationId;

                foreach (tblClassification classification in classificationList)

                {

                    if (classification.ClassificationId == person.PersonClassificationId)

                    {

                        personClassificationCompanyCityCountryPosition.ClassificationName = classification.ClassificationName;

                        break;

                    }

                }

                personClassificationCompanyCityCountryPosition.CompanyId = person.PersonCompanyId;

                foreach (tblCompany company in companyList)

                {

                    if (company.CompanyId == person.PersonCompanyId)

                    {

                        personClassificationCompanyCityCountryPosition.CompanyName = company.CompanyName;

                        break;

                    }

                }

                personClassificationCompanyCityCountryPosition.CountryId = person.PersonHomeCountryId;

                foreach (tblCountry country in countryList)

                {

                    if (country.CountryId == person.PersonHomeCountryId)

                    {

                        personClassificationCompanyCityCountryPosition.CountryName = country.CountryName;

                        break;

                    }

                }

                personClassificationCompanyCityCountryPosition.PersonBirthDate = person.PersonBirthDate;

                personClassificationCompanyCityCountryPosition.PersonFirstName = person.PersonFirstName;

                personClassificationCompanyCityCountryPosition.PersonGender = person.PersonGender;

                personClassificationCompanyCityCountryPosition.PersonHomeAddress = person.PersonHomeAddress;

                personClassificationCompanyCityCountryPosition.PersonHomeCellularPhone = person.PersonHomeCellularPhone;

                personClassificationCompanyCityCountryPosition.PersonHomeEmail = person.PersonHomeEmail;

                personClassificationCompanyCityCountryPosition.PersonHomePhone = person.PersonHomePhone;

                personClassificationCompanyCityCountryPosition.PersonID = person.PersonId;

                personClassificationCompanyCityCountryPosition.PersonLastName = person.PersonLastName;

                personClassificationCompanyCityCountryPosition.PersonMiddleName = person.PersonMiddleName;

                personClassificationCompanyCityCountryPosition.PersonName = person.PersonFirstName + ” ” +  person.PersonMiddleName + ” ” + person.PersonLastName;

                personClassificationCompanyCityCountryPosition.PositionId = person.PersonPositionId;

                foreach (tblPosition position in positionList)

                {

                    if (position.PositionId == person.PersonPositionId)

                    {

                        personClassificationCompanyCityCountryPosition.PositionName = position.PositionName;

                        break;

                    }

                }

                if (!list.Exists(pccccp => pccccp.PersonID == personClassificationCompanyCityCountryPosition.PersonID))

                    list.Add(personClassificationCompanyCityCountryPosition);

            }

            if (sortDirection == “ascending”)

            {

                switch (sortExpression)

                {

                    case “CityName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.CityName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “ClassificationName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.ClassificationName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “CompanyName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.CompanyName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “CountryName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.CountryName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonBirthDate”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonBirthDate).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonFirstName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonFirstName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonGender”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonGender).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeAddress”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonHomeAddress).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeCellularPhone”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonHomeCellularPhone).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeEmail”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonHomeEmail).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomePhone”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonHomePhone).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonLastName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonLastName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonMiddleName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonMiddleName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PersonName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PositionName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderBy(p => p.PositionName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                }

            }

            else

            {

                switch (sortExpression)

                {

                    case “CityName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.CityName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “ClassificationName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.ClassificationName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “CompanyName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.CompanyName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “CountryName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.CountryName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonBirthDate”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonBirthDate).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonFirstName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonFirstName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonGender”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonGender).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeAddress”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonHomeAddress).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeCellularPhone”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonHomeCellularPhone).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomeEmail”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonHomeEmail).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonHomePhone”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonHomePhone).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonLastName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonLastName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonMiddleName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonMiddleName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PersonName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PersonName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                    case “PositionName”:

                        personClassificationCompanyCityCountryPositionList = list.OrderByDescending(p => p.PositionName).ToList<PersonClassificationCompanyCityCountryPosition>();

                        break;

                }

            }

            return personClassificationCompanyCityCountryPositionList;

        }

        public IOrderedQueryable RetrievePersonQueryListByName(string name , string sortExpression, string sortDirection)

        {

            var data = (from p in dbEntity.tblPersons

                        join cl in dbEntity.tblClassifications

                        on p.PersonClassificationId equals cl.ClassificationId

                        join com in dbEntity.tblCompanies

                        on p.PersonCompanyId equals com.CompanyId

                        join c in dbEntity.tblCities

                        on p.PersonHomeCityId equals c.CityId

                        join co in dbEntity.tblCountries

                        on p.PersonHomeCountryId equals co.CountryId

                        join po in dbEntity.tblPositions

                        on p.PersonPositionId equals po.PositionId

                        where (p.PersonFirstName + ” ” + p.PersonMiddleName  + ” ” + p.PersonLastName).Contains(name)

                        orderby sortExpression + ” ” + sortDirection

                        select new

                        {

                            p.PersonBirthDate,

                            p.PersonClassificationId,

                            cl.ClassificationName,

                            p.PersonCompanyId,

                            com.CompanyName,

                            p.PersonFirstName,

                            p.PersonGender,

                            p.PersonHomeAddress,

                            p.PersonHomeCellularPhone,

                            p.PersonHomeCityId,

                            c.CityName,

                            p.PersonHomeCountryId,

                            co.CountryName,

                            p.PersonHomeEmail,

                            p.PersonHomePhone,

                            p.PersonId,

                            p.PersonLastName,

                            p.PersonMiddleName,

                            p.PersonPositionId,

                            po.PositionName,

                            PersonName = p.PersonFirstName + ” ” + p.PersonMiddleName + ” ” + p.PersonLastName

                        });

            var orderData = data.OrderBy(p=>p.PersonName);

            if (sortDirection == “ascending”)

            {

                switch (sortExpression)

                {

                    case “CityName”:

                        return data.OrderBy(p => p.CityName);

                    case “ClassificationName”:

                        return data.OrderBy(p => p.ClassificationName);

                    case “CompanyName”:

                        return data.OrderBy(p => p.CompanyName);

                    case “CountryName”:

                        return data.OrderBy(p => p.CountryName);

                    case “PersonBirthDate”:

                        return data.OrderBy(p => p.PersonBirthDate);

                    case “PersonFirstName”:

                        return data.OrderBy(p => p.PersonFirstName);

                    case “PersonGender”:

                        return data.OrderBy(p => p.PersonGender);

                    case “PersonHomeAddress”:

                        return data.OrderBy(p => p.PersonHomeAddress);

                    case “PersonHomeCellularPhone”:

                        return data.OrderBy(p => p.PersonHomeCellularPhone);

                    case “PersonHomeEmail”:

                        return data.OrderBy(p => p.PersonHomeEmail);

                    case “PersonHomePhone”:

                        return data.OrderBy(p => p.PersonHomePhone);

                    case “PersonLastName”:

                        return data.OrderBy(p => p.PersonLastName);

                    case “PersonMiddleName”:

                        return data.OrderBy(p => p.PersonMiddleName);

                    case “PersonName”:

                        return data.OrderBy(p => p.PersonName);

                    case “PositionName”:

                        return data.OrderBy(p => p.PositionName);

                }

            }

            else

            {

                switch (sortExpression)

                {

                    case “CityName”:

                        return data.OrderByDescending(p => p.CityName);

                    case “ClassificationName”:

                        return data.OrderByDescending(p => p.ClassificationName);

                    case “CompanyName”:

                        return data.OrderByDescending(p => p.CompanyName);

                    case “CountryName”:

                        return data.OrderByDescending(p => p.CountryName);

                    case “PersonBirthDate”:

                        return data.OrderByDescending(p => p.PersonBirthDate);

                    case “PersonFirstName”:

                        return data.OrderByDescending(p => p.PersonFirstName);

                    case “PersonGender”:

                        return data.OrderByDescending(p => p.PersonGender);

                    case “PersonHomeAddress”:

                        return data.OrderByDescending(p => p.PersonHomeAddress);

                    case “PersonHomeCellularPhone”:

                        return data.OrderByDescending(p => p.PersonHomeCellularPhone);

                    case “PersonHomeEmail”:

                        return data.OrderByDescending(p => p.PersonHomeEmail);

                    case “PersonHomePhone”:

                        return data.OrderByDescending(p => p.PersonHomePhone);

                    case “PersonLastName”:

                        return data.OrderByDescending(p => p.PersonLastName);

                    case “PersonMiddleName”:

                        return data.OrderByDescending(p => p.PersonMiddleName);

                    case “PersonName”:

                        return data.OrderByDescending(p => p.PersonName);

                    case “PositionName”:

                        return data.OrderByDescending(p => p.PositionName);

                }

            }

            return personQuery;

        }

    }

    public class PersonClass : PersonInterface

    {

        List<tblPerson> personList;

        tblPerson person;

        TestDBEntitiesConnectionString dbEntity;

        string connString = ConfigurationManager.ConnectionStrings[“TestDBEntitiesConnectionString”].ConnectionString;

        public PersonClass()

        {

            personList = new List<tblPerson>();

            person = new tblPerson();

            dbEntity = new TestDBEntitiesConnectionString(connString);

        }

        public List<tblPerson> RetrievePersonList()

        {

            personList = (from p in dbEntity.tblPersons

                          select p).ToList<tblPerson>();

            return personList;

        }

        public List<tblPerson> RetrievePersonListByName(string name, string sortExpression, string sortDirection)

        {

            List<tblPerson> list = (from p in dbEntity.tblPersons

                          where (p.PersonFirstName + ” ” + p.PersonMiddleName + ” ” + p.PersonLastName).Contains(name)

                          select p).ToList<tblPerson>();

            if (sortDirection == “ascending”)

            {

                switch (sortExpression)

                {

                    case “PersonBirthDate”:

                        personList = list.OrderBy(p => p.PersonBirthDate).ToList<tblPerson>();

                        break;

                    case “PersonFirstName”:

                        personList = list.OrderBy(p => p.PersonFirstName).ToList<tblPerson>();

                        break;

                    case “PersonGender”:

                        personList = list.OrderBy(p => p.PersonGender).ToList<tblPerson>();

                        break;

                    case “PersonHomeAddress”:

                        personList = list.OrderBy(p => p.PersonHomeAddress).ToList<tblPerson>();

                        break;

                    case “PersonHomeCellularPhone”:

                        personList = list.OrderBy(p => p.PersonHomeCellularPhone).ToList<tblPerson>();

                        break;

                    case “PersonHomeEmail”:

                        personList = list.OrderBy(p => p.PersonHomeEmail).ToList<tblPerson>();

                        break;

                    case “PersonHomePhone”:

                        personList = list.OrderBy(p => p.PersonHomePhone).ToList<tblPerson>();

                        break;

                    case “PersonLastName”:

                        personList = list.OrderBy(p => p.PersonLastName).ToList<tblPerson>();

                        break;

                    case “PersonMiddleName”:

                        personList = list.OrderBy(p => p.PersonMiddleName).ToList<tblPerson>();

                        break;

                }

            }

            else

            {

                switch (sortExpression)

                {

                    case “PersonBirthDate”:

                        personList = list.OrderByDescending(p => p.PersonBirthDate).ToList<tblPerson>();

                        break;

                    case “PersonFirstName”:

                        personList = list.OrderByDescending(p => p.PersonFirstName).ToList<tblPerson>();

                        break;

                    case “PersonGender”:

                        personList = list.OrderByDescending(p => p.PersonGender).ToList<tblPerson>();

                        break;

                    case “PersonHomeAddress”:

                        personList = list.OrderByDescending(p => p.PersonHomeAddress).ToList<tblPerson>();

                        break;

                    case “PersonHomeCellularPhone”:

                        personList = list.OrderByDescending(p => p.PersonHomeCellularPhone).ToList<tblPerson>();

                        break;

                    case “PersonHomeEmail”:

                        personList = list.OrderByDescending(p => p.PersonHomeEmail).ToList<tblPerson>();

                        break;

                    case “PersonHomePhone”:

                        personList = list.OrderByDescending(p => p.PersonHomePhone).ToList<tblPerson>();

                        break;

                    case “PersonLastName”:

                        personList = list.OrderByDescending(p => p.PersonLastName).ToList<tblPerson>();

                        break;

                    case “PersonMiddleName”:

                        personList = list.OrderByDescending(p => p.PersonMiddleName).ToList<tblPerson>();

                        break;

                }

            }

            return personList;

        }

        public tblPerson RetrievePersonByID(int id)

        {

            person = (from p in dbEntity.tblPersons

                      where p.PersonId == id

                      select p).First<tblPerson>();

            return person;

        }

        public tblPerson RetrievePersonByName(string name)

        {

            person = (from p in dbEntity.tblPersons

                      where (p.PersonFirstName + ” ” + p.PersonMiddleName + ” ” + p.PersonLastName) == name

                      select p).First<tblPerson>();

            return person;

        }

        public int CreatePerson(tblPerson persons)

        {

            try

            {

                dbEntity.AddTotblPersons(persons);

                dbEntity.SaveChanges();

                person = (from p in dbEntity.tblPersons

                          where p.PersonLastName == persons.PersonLastName & p.PersonMiddleName == persons.PersonMiddleName & p.PersonFirstName == persons.PersonFirstName

                          select p).First<tblPerson>();

            }

            catch (System.Exception ex)

            {

                throw ex;

            }

            return person == null ? 0 : person.PersonId;

        }

        public int UpdatePerson(tblPerson persons)

        {

            person = (from p in dbEntity.tblPersons

                       where p.PersonId == persons.PersonId

                       select p).First<tblPerson>();

            person.PersonBirthDate = persons.PersonBirthDate;

            person.PersonClassificationId = persons.PersonClassificationId;

            person.PersonCompanyId = persons.PersonCompanyId;

            person.PersonFirstName = persons.PersonFirstName;

            person.PersonGender = persons.PersonGender;

            person.PersonHomeAddress = persons.PersonHomeAddress;

            person.PersonHomeCellularPhone = persons.PersonHomeCellularPhone;

            person.PersonHomeCityId = persons.PersonHomeCityId;

            person.PersonHomeCountryId = persons.PersonHomeCountryId;

            person.PersonHomeEmail = persons.PersonHomeEmail;

            person.PersonHomePhone = persons.PersonHomePhone;

            person.PersonLastName = persons.PersonLastName;

            person.PersonMiddleName = persons.PersonMiddleName;

            person.PersonPositionId = persons.PersonPositionId;

            return dbEntity.SaveChanges();

        }

        public int DeletePerson(int id)

        {

            person = (from p in dbEntity.tblPersons

                       where p.PersonId == id

                       select p).First<tblPerson>();

            dbEntity.DeleteObject(person);

            return dbEntity.SaveChanges();

        }

    }

}

To create the UI Part, here is the markup for designer part,

<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” CodeFile=”PersonPage.aspx.cs” Inherits=”PersonPage” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”HeadContent” Runat=”Server”>

</asp:Content>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” Runat=”Server”>

    <asp:ScriptManager ID=”ScriptManager” runat=”server” ></asp:ScriptManager>

    <asp:UpdatePanel ID=”upPerson” runat=”server” UpdateMode=”Conditional”>

        <ContentTemplate>

            <table>

                <tr>

                    <td>

                    </td>

                    <td>

                    </td>

                    <td>

                    </td>

                    <td>

                    </td>

                </tr>

                <tr>

                    <td>

                    </td>

                    <td colspan=”2″ align=”center”>

                        <asp:MultiView ID=”mvPerson” runat=”server” ActiveViewIndex=”0″>

                            <asp:View ID=”vwCreatePerson” runat=”server”>

                                <table width=”100%”>

                                    <tr>

                                        <td>

                                        </td>

                                        <td colspan=”2″>

                                            <asp:ValidationSummary ID=”vsCreatePerson” runat=”server”

                                                ValidationGroup=”validateCreatePerson” DisplayMode=”BulletList”

                                                ForeColor=”Red” />

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            First Name:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtFirstName” runat=”server” MaxLength=”20″ Width=”250px” ></asp:TextBox>

                                            <asp:RequiredFieldValidator ID=”rfvFirstName” runat=”server”

                                                ControlToValidate=”txtFirstName” ErrorMessage=”First name is required.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”>*</asp:RequiredFieldValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Last Name:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtLastName” runat=”server” MaxLength=”20″ Width=”250px”></asp:TextBox>

                                            <asp:RequiredFieldValidator ID=”rfvLastName” runat=”server”

                                                ControlToValidate=”txtLastName” ErrorMessage=”Last Name is required.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”>*</asp:RequiredFieldValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Middle Name:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtMiddleName” runat=”server” MaxLength=”20″ Width=”250px”></asp:TextBox>

                                            <asp:RequiredFieldValidator ID=”rfvMiddleName” runat=”server”

                                                ControlToValidate=”txtMiddleName” ErrorMessage=”Middle Name is required.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”>*</asp:RequiredFieldValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Gender:

                                        </td>

                                        <td align=”left”>

                                            <asp:RadioButtonList ID=”rdlGender” runat=”server” Width=”250px”>

                                                <asp:ListItem Text=”Male” Value=”M”></asp:ListItem>

                                                <asp:ListItem Text=”Female” Value=”F”></asp:ListItem>

                                            </asp:RadioButtonList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Birth Date:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtBirthDate” runat=”server” Width=”250px”></asp:TextBox>

                                            <asp:RequiredFieldValidator ID=”rfvBirthDate” runat=”server”

                                                ControlToValidate=”txtBirthDate” ErrorMessage=”Birth Date is required.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”>*</asp:RequiredFieldValidator>

                                            <asp:CompareValidator ID=”cvBirthDate” runat=”server”

                                                ControlToValidate=”txtBirthDate” ErrorMessage=” Birth Date is not valid date.”

                                                ForeColor=”Red”  ValidationGroup=”validateCreatePerson”>*</asp:CompareValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Address:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtAddress” runat=”server” TextMode=”MultiLine” Width=”250px”></asp:TextBox>

                                            <asp:RequiredFieldValidator ID=”rfvAddress” runat=”server”

                                                ControlToValidate=”txtAddress” ErrorMessage=”Address is required.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”>*</asp:RequiredFieldValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            City:

                                        </td>

                                        <td align=”left”>

                                            <asp:DropDownList ID=”ddlCity” runat=”server” Width=”250px”></asp:DropDownList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Country:

                                        </td>

                                        <td align=”left”>

                                            <asp:DropDownList ID=”ddlCountry” runat=”server” Width=”250px”></asp:DropDownList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            E-Mail Address:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtEmailAddress” runat=”server” Width=”250px”></asp:TextBox>

                                            <asp:RegularExpressionValidator ID=”revEmailAddress” runat=”server”

                                                ControlToValidate=”txtEmailAddress” ErrorMessage=”E-mail Address is not valid.”

                                                ForeColor=”Red” ValidationGroup=”validateCreatePerson”

                                                ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”>*</asp:RegularExpressionValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Home Phone Number:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtHomePhoneNo” runat=”server” Width=”250px”></asp:TextBox>

                                            <asp:RegularExpressionValidator ID=”revPhoneNo” runat=”server” ValidationGroup=”validateCreatePerson”

                                                ControlToValidate=”txtHomePhoneNo” ErrorMessage=”Phone number is not in correct format.”

                                                ForeColor=”Red” ValidationExpression=”((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}”>*</asp:RegularExpressionValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Cellular Phone Number:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtCellularPhoneNo” runat=”server” Width=”250px”></asp:TextBox>

                                            <asp:RegularExpressionValidator ID=”revCellularNo” runat=”server”

                                                ErrorMessage=”Cellular Number is not in correct format.” ValidationGroup=”validateCreatePerson”

                                                ControlToValidate=”txtCellularPhoneNo” ForeColor=”Red”

                                                ValidationExpression=”((\d{3} ?)|(\d{3}-))?\d{3}-\d{4}”>*</asp:RegularExpressionValidator>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Classification:

                                        </td>

                                        <td align=”left”>

                                            <asp:DropDownList ID=”ddlClassification” runat=”server” Width=”250px”></asp:DropDownList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Company:

                                        </td>

                                        <td align=”left”>

                                            <asp:DropDownList ID=”ddlCompany” runat=”server” Width=”250px”></asp:DropDownList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Position:

                                        </td>

                                        <td align=”left”>

                                            <asp:DropDownList ID=”ddlPosition” runat=”server” Width=”250px”></asp:DropDownList>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td align=”left”>

                                            <asp:Button ID=”btnSavePerson” runat=”server” Text=”Save”

                                                ValidationGroup=”validateCreatePerson” onclick=”btnSavePerson_Click” />

                                            <asp:Button ID=”btnCancelPerson” runat=”server” Text=”Cancel”

                                                onclick=”btnCancelPerson_Click” />

                                            <asp:Button ID=”btnShowSearchPerson” runat=”server” Text=”Search Person”

                                                onclick=”btnShowSearchPerson_Click” />

                                        </td>

                                        <td

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                </table>

                            </asp:View>

                            <asp:View ID=”vwSearchPerson” runat=”server”>

                                <table>

                                    <tr>

                                        <td>

                                        </td>

                                        <td colspan=”2″>

                                            <asp:ValidationSummary ID=”vsSearchPerson” runat=”server” DisplayMode=”BulletList” ValidationGroup=”validateSearchPerson” />

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td align=”right”>

                                            Search Person:

                                        </td>

                                        <td align=”left”>

                                            <asp:TextBox ID=”txtSearchPerson” runat=”server”></asp:TextBox>

                                            <asp:Button ID=”btnSearchPerson” runat=”server” Text=”Search”

                                                onclick=”btnSearchPerson_Click” />

                                            <asp:Button ID=”btnCreatePerson” runat=”server” Text=”Create Person”

                                                onclick=”btnCreatePerson_Click” />

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td colspan=”2″>

                                            <asp:GridView ID=”gvPerson” runat=”server” AllowPaging=”True”

                                                AllowSorting=”True” AutoGenerateColumns=”False”

                                                onpageindexchanging=”gvPerson_PageIndexChanging”

                                                onrowcancelingedit=”gvPerson_RowCancelingEdit”

                                                onrowdeleting=”gvPerson_RowDeleting” onrowediting=”gvPerson_RowEditing”

                                                onrowupdating=”gvPerson_RowUpdating” onsorting=”gvPerson_Sorting”>

                                                <Columns>

                                                    <asp:BoundField DataField=”PersonID” HeaderText=”Person ID” ReadOnly=”True”

                                                        SortExpression=”PersonID” Visible=”False” />

                                                    <asp:TemplateField HeaderText=”First Name” SortExpression=”PersonFirstName”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridFirstName” runat=”server” Text='<%# Bind(“PersonFirstName”) %>’ MaxLength=”20″></asp:TextBox>

                                                            <asp:RequiredFieldValidator ID=”rfvGridFirstName” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridFirstName” ErrorMessage=”First name is required.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:RequiredFieldValidator> 

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridFirstName” runat=”server” Text='<%# Bind(“PersonFirstName”) %>’></asp:Label>

                                                            <asp:TextBox ID=”txtGridFirstName” runat=”server” Text='<%# Bind(“PersonFirstName”) %>’ MaxLength=”20″ Visible=”false”></asp:TextBox>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Middle Name” SortExpression=”PersonMiddleName”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridMiddleName” runat=”server” Text='<%# Bind(“PersonMiddleName”) %>’ MaxLength=”20″></asp:TextBox>

                                                            <asp:RequiredFieldValidator ID=”rfvGridMiddleName” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridMiddleName” ErrorMessage=”Middle Name is required.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:RequiredFieldValidator>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridMiddleName” runat=”server” Text='<%# Bind(“PersonMiddleName”) %>’></asp:Label>

                                                            <asp:TextBox ID=”txtGridMiddleName” runat=”server” Text='<%# Bind(“PersonMiddleName”) %>’ MaxLength=”20″ Visible=”false”></asp:TextBox>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Last Name” SortExpression=”PersonLastName”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridLastName” runat=”server” Text='<%# Bind(“PersonLastName”) %>’ MaxLength=”20″></asp:TextBox>

                                                            <asp:RequiredFieldValidator ID=”rfvGridLastName” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridLastName” ErrorMessage=”Last Name is required.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:RequiredFieldValidator>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridLastName” runat=”server” Text='<%# Bind(“PersonLastName”) %>’></asp:Label>

                                                            <asp:TextBox ID=”txtGridLastName” runat=”server” Text='<%# Bind(“PersonLastName”) %>’ MaxLength=”20″ visible=”false”></asp:TextBox>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Gender” SortExpression=”PersonGender”>

                                                        <EditItemTemplate>

                                                            <asp:RadioButtonList ID=”rblGridGender” runat=”server”>

                                                                <asp:ListItem Text=”Male” Value=”M” ></asp:ListItem>

                                                                <asp:ListItem Text=”Female” Value=”F”></asp:ListItem>

                                                            </asp:RadioButtonList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridGender” runat=”server” Text='<%# Bind(“PersonGender”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Birth Date” SortExpression=”PersonBirthDate”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridBirthDate” runat=”server” Text='<%# Bind(“PersonBirthDate”) %>’></asp:TextBox>

                                                            <asp:RequiredFieldValidator ID=”rfvGridBirthDate” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridBirthDate” ErrorMessage=”Birth date is required.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:RequiredFieldValidator>

                                                            <asp:CompareValidator ID=”cvGridBirthDate” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridBirthDate” ErrorMessage=”Birth date is not valid date.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:CompareValidator>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridBirthDate” runat=”server” Text='<%# Bind(“PersonBirthDate”) %>’></asp:Label>

                                                            <asp:TextBox ID=”txtGridBirthDate” runat=”server” Text='<%# Bind(“PersonBirthDate”) %>’ Visible=”false”></asp:TextBox>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Address” SortExpression=”PersonHomeAddress”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridHomeAddress” runat=”server” Text='<%# Bind(“PersonHomeAddress”) %>’ TextMode=”MultiLine”></asp:TextBox>

                                                            <asp:RequiredFieldValidator ID=”rfvGridAddress” runat=”server” ForeColor=”Red” ControlToValidate=”txtGridHomeAddress” ErrorMessage=”Address is required.” Text=”*” ValidationGroup=”validateSearchPerson”></asp:RequiredFieldValidator>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”blGridAddress” runat=”server” Text='<%# Bind(“PersonHomeAddress”) %>’></asp:Label>

                                                            <asp:TextBox ID=”txtGridHomeAddress” runat=”server” Text='<%# Bind(“PersonHomeAddress”) %>’ TextMode=”MultiLine” Visible=”false”></asp:TextBox>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”City ID” SortExpression=”PersonHomeCityID”

                                                        Visible=”False”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridCityID” runat=”server” Text='<%# Bind(“CityID”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCityID” runat=”server” Text='<%# Bind(“CityID”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”City” SortExpression=”CityName”>

                                                        <EditItemTemplate>

                                                            <asp:DropDownList ID=”ddlGridCity” runat=”server” ></asp:DropDownList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCity” runat=”server” Text='<%# Bind(“CityName”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Country ID” SortExpression=”PersonHomeCountryID”

                                                        Visible=”False”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridCountryID” runat=”server” Text='<%# Bind(“CountryID”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCountryID” runat=”server” Text='<%# Bind(“CountryID”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Country” SortExpression=”CountryName”>

                                                        <EditItemTemplate>

                                                            <asp:DropDownList ID=”ddlGridCountry” runat=”server” ></asp:DropDownList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCountry” runat=”server” Text='<%# Bind(“CountryName”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”E-mail Address” SortExpression=”PersonHomeEmail”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridEmail” runat=”server” Text='<%# Bind(“PersonHomeEmail”) %>’></asp:TextBox>

                                                            <asp:RegularExpressionValidator ID=”revGridEmailAddress” runat=”server”

                                                ControlToValidate=”txtGridEmail” ErrorMessage=”E-mail Address is not valid.”

                                                ForeColor=”Red” ValidationGroup=”validateSearchPerson”

                                                ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”>*</asp:RegularExpressionValidator>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridEmail” runat=”server” Text='<%# Bind(“PersonHomeEmail”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Phone No” SortExpression=”PersonHomePhone”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridHomePhone” runat=”server” Text='<%# Bind(“PersonHomePhone”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridHomePhone” runat=”server” Text='<%# Bind(“PersonHomePhone”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Cellular Phone”

                                                        SortExpression=”PersonHomeCellularPhone”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridCellularPhone” runat=”server” Text='<%# Bind(“PersonHomeCellularPhone”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCellularPhone” runat=”server” Text='<%# Bind(“PersonHomeCellularPhone”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Classification ID”

                                                        SortExpression=”PersonClassificationID” Visible=”False”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridClassificationID” runat=”server” Text='<%# Bind(“ClassificationID”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridClassificationID” runat=”server” Text='<%# Bind(“ClassificationID”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Classification”

                                                        SortExpression=”ClassificationName”>

                                                        <EditItemTemplate>

                                                            <asp:DropDownList ID=”ddlGridClassification” runat=”server” ></asp:DropDownList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridClassification” runat=”server” Text='<%# Bind(“ClassificationName”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Company ID” SortExpression=”PersonCompanyID”

                                                        Visible=”False”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridCompanyID” runat=”server” Text='<%# Bind(“CompanyID”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCompanyID” runat=”server” Text='<%# Bind(“CompanyID”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Company” SortExpression=”CompanyName”>

                                                        <EditItemTemplate>

                                                            <asp:DropDownList ID=”ddlGridCompany” runat=”server” ></asp:DropDownList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridCompany” runat=”server” Text='<%# Bind(“CompanyName”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Position ID” SortExpression=”PersonPositionID”

                                                        Visible=”False”>

                                                        <EditItemTemplate>

                                                            <asp:TextBox ID=”txtGridPositionID” runat=”server” Text='<%# Bind(“PositionID”) %>’></asp:TextBox>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridPositionID” runat=”server” Text='<%# Bind(“PositionID”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText=”Position” SortExpression=”PositionName”>

                                                        <EditItemTemplate>

                                                            <asp:DropDownList ID=”ddlGridPosition” runat=”server”></asp:DropDownList>

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Label ID=”lblGridPosition” runat=”server” Text='<%# Bind(“PositionName”) %>’></asp:Label>

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField ShowHeader=”False”>

                                                        <EditItemTemplate>

                                                            <asp:Button ID=”btnGridUpdatePerson” runat=”server” CausesValidation=”True”

                                                                CommandName=”Update” Text=”Update” ValidationGroup=”validateSearchPerson” />

                                                            &nbsp;<asp:Button ID=”btnGridCancelPerson” runat=”server” CausesValidation=”False”

                                                                CommandName=”Cancel” Text=”Cancel” />

                                                        </EditItemTemplate>

                                                        <ItemTemplate>

                                                            <asp:Button ID=”btnGridEditPerson” runat=”server” CausesValidation=”False”

                                                                CommandName=”Edit” Text=”Edit” />

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                    <asp:TemplateField ShowHeader=”False”>

                                                        <ItemTemplate>

                                                            <asp:Button ID=”btnGridDeletePerson” runat=”server” CausesValidation=”False”

                                                                CommandName=”Delete” Text=”Delete” />

                                                            <asp:HiddenField ID=”hfPersonID” runat=”server” Value='<%# Bind(“PersonID”) %>’ />

                                                        </ItemTemplate>

                                                    </asp:TemplateField>

                                                </Columns>

                                            </asp:GridView>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                        <td>

                                        </td>

                                    </tr>

                                </table>

                            </asp:View>

                        </asp:MultiView>

                    </td>

                    <td>

                    </td>

                </tr>

                <tr>

                    <td>

                    </td>

                    <td>

                    </td>

                    <td>

                    </td>

                    <td>

                    </td>

                </tr>

            </table>

        </ContentTemplate>

        <Triggers>

        </Triggers>

    </asp:UpdatePanel>

</asp:Content>

Here is the code part,

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using MyLinqToEntityClassLibrary;

public partial class PersonPage : System.Web.UI.Page

{

    #region “Variables”

    List<tblCity> cityList;

    List<tblClassification> classificationList;

    List<tblCompany> companyList;

    List<tblCountry> countryList;

    List<tblPerson> personList;

    List<tblPosition> positionList;

    tblPerson person;

    CityClass cityBll;

    CountryClass countryBll;

    CompanyClass companyBll;

    ClassificationClass classificationBll;

    PersonClass personBll;

    PositionClass positionBll;

    IOrderedQueryable personQry;

    List<PersonClassificationCompanyCityCountryPosition> personClassificationCompanyCityCountryPositionList;

    PersonClassificationCompanyCityCountryPositionBLL personClassificationCompanyCityCountryPositionBll;

    #endregion

    #region “Methods”

    private void MessageBox(string message)

    {

        ScriptManager.RegisterStartupScript(this, this.GetType(), “onclick”, “alert(‘” + message + “‘)”, true);

    }

    private void ResetEntry()

    {

        this.txtAddress.Text = string.Empty;

        this.txtBirthDate.Text = string.Empty;

        this.txtCellularPhoneNo.Text = string.Empty;

        this.txtEmailAddress.Text = string.Empty;

        this.txtFirstName.Text = string.Empty;

        this.txtHomePhoneNo.Text = string.Empty;

        this.txtLastName.Text = string.Empty;

        this.txtMiddleName.Text = string.Empty;

        this.txtSearchPerson.Text = string.Empty;

        this.ddlCity.SelectedIndex = -1;

        this.ddlClassification.SelectedIndex = -1;

        this.ddlCompany.SelectedIndex = -1;

        this.ddlCountry.SelectedIndex = -1;

        this.ddlPosition.SelectedIndex = -1;

    }

    private void PopulateGridView()

    {

        try

        {

            personClassificationCompanyCityCountryPositionList = personClassificationCompanyCityCountryPositionBll.RetrievePersonNameListByName(this.txtSearchPerson.Text.Trim(), Convert.ToString(Session[“SortExpression”]), Convert.ToString(Session[“SortDirection”]));

            this.gvPerson.DataSource = personClassificationCompanyCityCountryPositionList;

            this.gvPerson.DataBind();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void SetGender(RadioButtonList rbl, string gender)

    {

        rbl.SelectedValue = gender;

    }

    private void PopulateCityList()

    {

        try

        {

            cityList = cityBll.RetrieveCityList();

            this.ddlCity.DataSource = cityList;

            this.ddlCity.DataTextField = “CityName”;

            this.ddlCity.DataValueField = “CityID”;

            this.ddlCity.DataBind(); 

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateGridCityList(DropDownList ddl, string id)

    {

        try

        {

            cityList = cityBll.RetrieveCityList();

            ddl.DataSource = cityList;

            ddl.DataTextField = “CityName”;

            ddl.DataValueField = “CityID”;

            ddl.DataBind();

            ddl.SelectedValue = id;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateCountryList()

    {

        try

        {

            countryList = countryBll.RetrieveCountryList();

            this.ddlCountry.DataSource = countryList;

            this.ddlCountry.DataTextField = “CountryName”;

            this.ddlCountry.DataValueField = “CountryID”;

            this.ddlCountry.DataBind();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateGridCountryList(DropDownList ddl, string id)

    {

        try

        {

            countryList = countryBll.RetrieveCountryList();

            ddl.DataSource = countryList;

            ddl.DataTextField = “CountryName”;

            ddl.DataValueField = “CountryID”;

            ddl.DataBind();

            ddl.SelectedValue = id;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateCompanyList()

    {

        try

        {

            companyList = companyBll.RetrieveCompanyList();

            this.ddlCompany.DataSource = companyList;

            this.ddlCompany.DataTextField = “CompanyName”;

            this.ddlCompany.DataValueField = “CompanyID”;

            this.ddlCompany.DataBind(); 

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateGridCompanyList(DropDownList ddl, string id)

    {

        try

        {

            companyList = companyBll.RetrieveCompanyList();

            ddl.DataSource = companyList;

            ddl.DataTextField = “CompanyName”;

            ddl.DataValueField = “CompanyID”;

            ddl.DataBind();

            ddl.SelectedValue = id;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateClassificationList()

    {

        try

        {

            classificationList = classificationBll.RetrieveClassificationList();

            this.ddlClassification.DataSource = classificationList;

            this.ddlClassification.DataTextField = “ClassificationName”;

            this.ddlClassification.DataValueField = “ClassificationID”;

            this.ddlClassification.DataBind();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateGridClassificationList(DropDownList ddl, string id)

    {

        try

        {

            classificationList = classificationBll.RetrieveClassificationList();

            ddl.DataSource = classificationList;

            ddl.DataTextField = “ClassificationName”;

            ddl.DataValueField = “ClassificationID”;

            ddl.DataBind();

            ddl.SelectedValue = id;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulatePositionList()

    {

        try

        {

            positionList = positionBll.RetrievePositionList();

            this.ddlPosition.DataSource = positionList;

            this.ddlPosition.DataTextField = “PositionName”;

            this.ddlPosition.DataValueField = “PositionID”;

            this.ddlPosition.DataBind();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private void PopulateGridPositionList(DropDownList ddl, string id)

    {

        try

        {

            positionList = positionBll.RetrievePositionList();

            ddl.DataSource = positionList;

            ddl.DataTextField = “PositionName”;

            ddl.DataValueField = “PositionID”;

            ddl.DataBind();

            ddl.SelectedValue = id;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    private bool CheckForDuplicatePerson(string name, int id, bool IsEdit)

    {

        bool duplicate = false;

        try

        {

            personList = personBll.RetrievePersonListByName(name, Convert.ToString(Session[“SortExpression”]), Convert.ToString(Session[“SortDirection”]));

            if (IsEdit)

            {

                person = personList.Find(p => p.PersonId != id & (p.PersonFirstName + ” ” + p.PersonMiddleName + ” ” + p.PersonLastName) == name);

                if (person != null)

                    duplicate = true;

            }

            else

            {

                if (personList != null & personList.Count > 0)

                    duplicate = true;

            }

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

        return duplicate;

    }

    private bool CheckForDependency(int id)

    {

        bool dependency = false;

        try

        {

            if (companyList == null | companyList.Count == 0 )

                companyList = companyBll.RetrieveCompanyList();

            tblCompany company = companyList.Find(c => c.CompanyContactPersonId == id);

            if (company != null)

                dependency = true;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

        return dependency;

    }

    #endregion

    #region “Events”

    public void Page_Init(object sender, EventArgs e)

    {

        cityList = new List<tblCity>();

        classificationList = new List<tblClassification>();

        companyList = new List<tblCompany>();

        countryList = new List<tblCountry>();

        personList = new List<tblPerson>();

        positionList = new List<tblPosition>();

        person = new tblPerson();

        cityBll = new CityClass();

        countryBll = new CountryClass();

        companyBll = new CompanyClass();

        classificationBll = new ClassificationClass();

        personBll = new PersonClass();

        positionBll = new PositionClass();

        personClassificationCompanyCityCountryPositionList = new List<PersonClassificationCompanyCityCountryPosition>();

        personClassificationCompanyCityCountryPositionBll = new PersonClassificationCompanyCityCountryPositionBLL();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        try

        {

            if (!this.Page.IsPostBack)

            {

                PopulateCityList();

                PopulateClassificationList();

                PopulateCompanyList();

                PopulateCountryList();

                PopulatePositionList();

                this.cvBirthDate.Operator = ValidationCompareOperator.LessThanEqual;

                this.cvBirthDate.ValueToCompare = DateTime.Now.AddYears(-18).ToShortDateString();

                Response.Write(DateTime.Now.AddYears(-18).ToShortDateString());

            }

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void btnSavePerson_Click(object sender, EventArgs e)

    {

        try

        {

            person = new tblPerson();

            person.PersonBirthDate = Convert.ToDateTime(this.txtBirthDate.Text);

            person.PersonClassificationId = Convert.ToInt32(this.ddlClassification.SelectedValue);

            person.PersonCompanyId = Convert.ToInt32(this.ddlCompany.SelectedValue);

            person.PersonFirstName = this.txtFirstName.Text.Trim();

            person.PersonGender = this.rdlGender.SelectedValue;

            person.PersonHomeAddress = this.txtAddress.Text.Trim();

            person.PersonHomeCellularPhone = this.txtCellularPhoneNo.Text.Trim().Replace(” “, “”).Replace(“-“, “”);

            person.PersonHomeCityId = Convert.ToInt32(this.ddlCity.SelectedValue);

            person.PersonHomeCountryId = Convert.ToInt32(this.ddlCountry.SelectedValue);

            person.PersonHomeEmail = this.txtEmailAddress.Text.Trim();

            person.PersonHomePhone = this.txtHomePhoneNo.Text.Trim().Replace(“(“,””).Replace(“)”,””).Replace(” “,””).Replace(“-“,””);

            person.PersonLastName = this.txtLastName.Text.Trim();

            person.PersonMiddleName = this.txtMiddleName.Text.Trim();

            person.PersonPositionId = Convert.ToInt32(this.ddlPosition.SelectedValue);

            if (CheckForDuplicatePerson(person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName, 0, false) == false)

            {

                int result = personBll.CreatePerson(person);

                if (result > 0)

                    MessageBox(“Successfully add new person, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

                else

                    MessageBox(“Failed to add new person, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

            }

            else

                MessageBox(“Duplicate person’s name, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void btnCancelPerson_Click(object sender, EventArgs e)

    {

        try

        {

            ResetEntry();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void btnShowSearchPerson_Click(object sender, EventArgs e)

    {

        try

        {

            this.mvPerson.ActiveViewIndex = 1;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void btnSearchPerson_Click(object sender, EventArgs e)

    {

        try

        {

            Session[“SortDirection”] = “ascending”;

            Session[“SortExpression”] = “PersonFirstName”;

            PopulateGridView();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void btnCreatePerson_Click(object sender, EventArgs e)

    {

        try

        {

            this.mvPerson.ActiveViewIndex = 0;

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        try

        {

            this.gvPerson.PageIndex = e.NewPageIndex;

            PopulateGridView();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        try

        {

            this.gvPerson.EditIndex = -1;

            PopulateGridView();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        try

        {

            HiddenField hf = (HiddenField)this.gvPerson.Rows[e.RowIndex].Cells[21].FindControl(“hfPersonID”);

            if (CheckForDependency(Convert.ToInt32(hf.Value)))

                MessageBox(“There is a dependency on other table. It cannot be deleted before deleting the child first.”);

            else

            {

                int result = personBll.DeletePerson(Convert.ToInt32(hf.Value));

                if (result > 0)

                    MessageBox(“Successfully deleted this person.”);

                else

                    MessageBox(“Failed to delete this person.”);

                PopulateGridView();

            }

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_RowEditing(object sender, GridViewEditEventArgs e)

    {

        try

        {

            this.gvPerson.EditIndex = e.NewEditIndex;

            PopulateGridView();

            RadioButtonList gender = (RadioButtonList)this.gvPerson.Rows[e.NewEditIndex].Cells[4].FindControl(“rblGridGender”);

            TextBox cityID = (TextBox)this.gvPerson.Rows[e.NewEditIndex].Cells[7].FindControl(“txtGridCityID”);

            DropDownList city = (DropDownList)this.gvPerson.Rows[e.NewEditIndex].Cells[8].FindControl(“ddlGridCity”);

            TextBox countryId = (TextBox)this.gvPerson.Rows[e.NewEditIndex].Cells[9].FindControl(“txtGridCountryID”);

            DropDownList country = (DropDownList)this.gvPerson.Rows[e.NewEditIndex].Cells[10].FindControl(“ddlGridCountry”);

            TextBox classificationId = (TextBox)this.gvPerson.Rows[e.NewEditIndex].Cells[14].FindControl(“txtGridClassificationID”);

            DropDownList classification = (DropDownList)this.gvPerson.Rows[e.NewEditIndex].Cells[15].FindControl(“ddlGridClassification”);

            TextBox companyId = (TextBox)this.gvPerson.Rows[e.NewEditIndex].Cells[16].FindControl(“txtGridCompanyID”);

            DropDownList company = (DropDownList)this.gvPerson.Rows[e.NewEditIndex].Cells[17].FindControl(“ddlGridCompany”);

            TextBox positionId = (TextBox)this.gvPerson.Rows[e.NewEditIndex].Cells[18].FindControl(“txtGridPositionID”);

            DropDownList position = (DropDownList)this.gvPerson.Rows[e.NewEditIndex].Cells[19].FindControl(“ddlGridPosition”);

            HiddenField hf = (HiddenField)this.gvPerson.Rows[e.NewEditIndex].Cells[21].FindControl(“hfPersonID”);

            RegularExpressionValidator revEmail = (RegularExpressionValidator)this.gvPerson.Rows[e.NewEditIndex].Cells[11].FindControl(“revGridEmailAddress”);

            CompareValidator cv = (CompareValidator)this.gvPerson.Rows[e.NewEditIndex].Cells[5].FindControl(“cvGridBirthDate”);

            cv.Operator = ValidationCompareOperator.LessThanEqual;

            cv.ValueToCompare = DateTime.Now.AddYears(-18).ToShortDateString();

            PopulateGridCityList(city, cityID.Text.Trim());

            PopulateGridClassificationList(classification, classificationId.Text.Trim());

            PopulateGridCompanyList(company, companyId.Text.Trim());

            PopulateGridCountryList(country, countryId.Text.Trim());

            PopulateGridPositionList(position, positionId.Text.Trim());

            if (personClassificationCompanyCityCountryPositionList == null)

                personClassificationCompanyCityCountryPositionList = personClassificationCompanyCityCountryPositionBll.RetrievePersonNameListByName(this.txtSearchPerson.Text.Trim(), Convert.ToString(Session[“SortExpression”]), Convert.ToString(Session[“SortDirection”]));

            foreach (PersonClassificationCompanyCityCountryPosition pp in personClassificationCompanyCityCountryPositionList)

            {

                if (pp.PersonID == Convert.ToInt32(hf.Value))

                    SetGender(gender, pp.PersonGender);

            }

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        try

        {

            TextBox firstname = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[1].FindControl(“txtGridFirstName”);

            TextBox middleName = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[2].FindControl(“txtGridMiddleName”);

            TextBox lastName = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[3].FindControl(“txtGridLastName”);

            RadioButtonList gender = (RadioButtonList)this.gvPerson.Rows[e.RowIndex].Cells[4].FindControl(“rblGridGender”);

            TextBox birthDate = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[5].FindControl(“txtGridBirthDate”);

            TextBox address = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[6].FindControl(“txtGridHomeAddress”);

            TextBox cityID = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[7].FindControl(“txtGridCityID”);

            DropDownList city = (DropDownList)this.gvPerson.Rows[e.RowIndex].Cells[8].FindControl(“ddlGridCity”);

            TextBox countryId = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[9].FindControl(“txtGridCountryID”);

            DropDownList country = (DropDownList)this.gvPerson.Rows[e.RowIndex].Cells[10].FindControl(“ddlGridCountry”);

            TextBox email = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[11].FindControl(“txtGridEmail”);

            TextBox phone = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[12].FindControl(“txtGridHomePhone”);

            TextBox cellular = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[13].FindControl(“txtGridCellularPhone”);

            TextBox classificationId = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[14].FindControl(“txtGridClassificationID”);

            DropDownList classification = (DropDownList)this.gvPerson.Rows[e.RowIndex].Cells[15].FindControl(“ddlGridClassification”);

            TextBox companyId = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[16].FindControl(“txtGridCompanyID”);

            DropDownList company = (DropDownList)this.gvPerson.Rows[e.RowIndex].Cells[17].FindControl(“ddlGridCompany”);

            TextBox positionId = (TextBox)this.gvPerson.Rows[e.RowIndex].Cells[18].FindControl(“txtGridPositionID”);

            DropDownList position = (DropDownList)this.gvPerson.Rows[e.RowIndex].Cells[19].FindControl(“ddlGridPosition”);

            HiddenField hf = (HiddenField)this.gvPerson.Rows[e.RowIndex].Cells[21].FindControl(“hfPersonID”);

            if (CheckForDuplicatePerson(firstname + ” ” + middleName + ” ” + lastName, Convert.ToInt32(hf.Value), true) == false)

            {

                person = personBll.RetrievePersonByID(Convert.ToInt32(hf.Value));

                person.PersonBirthDate = Convert.ToDateTime( birthDate.Text.Trim());

                person.PersonClassificationId = Convert.ToInt32(classification.SelectedValue);

                person.PersonCompanyId = Convert.ToInt32(company.SelectedValue);

                person.PersonFirstName = firstname.Text.Trim();

                person.PersonGender = gender.SelectedValue;

                person.PersonHomeAddress = address.Text.Trim();

                person.PersonHomeCellularPhone = cellular.Text.Trim();

                person.PersonHomeCityId = Convert.ToInt32(city.SelectedValue);

                person.PersonHomeCountryId = Convert.ToInt32(country.SelectedValue);

                person.PersonHomeEmail = email.Text.Trim();

                person.PersonHomePhone = phone.Text.Trim().Replace(“(“, “”).Replace(“)”, “”).Replace(“-“, “”).Replace(” “, “”);

                person.PersonId = Convert.ToInt32(hf.Value);

                person.PersonLastName = lastName.Text.Trim();

                person.PersonMiddleName = middleName.Text.Trim();

                person.PersonPositionId = Convert.ToInt32(position.SelectedValue);

                int result = personBll.UpdatePerson(person);

                if (result > 0)

                    MessageBox(” Successfully update this person, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

                else

                    MessageBox(“Failed to update this person, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

                this.gvPerson.EditIndex = -1;

                PopulateGridView();

            }

            else

                MessageBox(“Duplicate person, ” + person.PersonFirstName + ” ” + person.PersonMiddleName + ” ” + person.PersonLastName);

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    protected void gvPerson_Sorting(object sender, GridViewSortEventArgs e)

    {

        try

        {

            if (Convert.ToString(Session[“SortDirection”]) == “ascending”)

            {

                e.SortDirection = SortDirection.Descending;

                Session[“SortDirection”] = SortDirection.Descending.ToString();

            }

            else

            {

                e.SortDirection = SortDirection.Ascending;

                Session[“SortDirection”] = SortDirection.Ascending.ToString();

            }

            Session[“SortExpression”] = e.SortExpression;

            PopulateGridView();

        }

        catch (System.Exception ex)

        {

            MessageBox(ex.Message + ” ” + ex.StackTrace);

        }

    }

    #endregion

}

Advertisements
This entry was posted in .NET Framework 4, C# 4.0 AND ASP.NET 4.0, Linq to Entity. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s