protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Department d = new Department(); int departmentID = 0; if (Request.QueryString["DepartmentID"] != null) { departmentID = int.Parse(Request.QueryString["DepartmentID"]); d = (from obj in db.Departments where obj.DepartmentID == departmentID select obj).FirstOrDefault(); } d.Name = txtName.Text; d.Budget = decimal.Parse(txtBudget.Text); if (departmentID == 0) { db.Departments.Add(d); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("departments.aspx"); } }
protected void GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the StudentID from the URL parameter Student s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a match if (s != null) { txtLastName.Text = s.LastName; txtFirstMidName.Text = s.FirstMidName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); } //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent() var objE = (from en in db.Enrollments join c in db.Courses on en.CourseID equals c.CourseID join d in db.Departments on c.DepartmentID equals d.DepartmentID where en.StudentID == StudentID select new { en.EnrollmentID, en.Grade, c.Title, d.Name }); grdCourses.DataSource = objE.ToList(); grdCourses.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { //do insert or update using (comp2007Entities db = new comp2007Entities()) { Room objC = new Room(); if (!String.IsNullOrEmpty(Request.QueryString["RoomID"])) { Int32 RoomID = Convert.ToInt32(Request.QueryString["RoomID"]); objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); } //populate the room from the input form objC.Size = ddlRoomSize.Text; objC.CostPerDay = Convert.ToDecimal(txtCostPerDay.Text); objC.Availability = "y"; if (String.IsNullOrEmpty(Request.QueryString["RoomID"])) { //add db.Rooms.Add(objC); } //save and redirect db.SaveChanges(); Response.Redirect("rooms.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //do insert or update using (comp2007Entities db = new comp2007Entities()) { Room objC = new Room(); if (!String.IsNullOrEmpty(Request.QueryString["RoomID"])) { Int32 RoomID = Convert.ToInt32(Request.QueryString["RoomID"]); objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); } Booking objD = new Booking(); //populate the booking entry from the input form and room info objD.RoomID = objC.RoomID; objD.LastName = txtLastName.Text; objD.FirstName = txtFirstName.Text; objD.DaysStaying = Convert.ToInt32(txtDaysStaying.Text); objD.BookDate = DateTime.Today; //Decimal TotalCost = objC.CostPerDay * objD.DaysStaying; //add db.Bookings.Add(objD); //save and redirect db.SaveChanges(); Response.Redirect("bookings.aspx"); } }
protected void getCategoryDetail() { //populate form with existing student record Int32 CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the student ID from the url parameter Category c = (from objS in db.Categories where objS.CategoryID == CategoryID select objS).FirstOrDefault(); //map the student to the form controls when s found if (c != null) { txtCtgname.Text = c.CategoryName; if(c.isExpense){ ckbIsExpense.Checked = true; } if (c.isActive) { ckbActivated.Checked = true; } } } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Course c = new Course(); int courseID = 0; if (Request.QueryString["CourseID"] != null) { courseID = int.Parse(Request.QueryString["CourseID"]); c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); } c.Title = txtTitle.Text; c.Credits = int.Parse(txtCredits.Text); c.DepartmentID = int.Parse(ddlDepartment.SelectedValue); if (courseID == 0) { db.Courses.Add(c); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("courses.aspx"); } }
protected void getAccountMod() { // Int32 AccountID = Convert.ToInt32(Request.QueryString["AccountID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { // Account a = (from objS in db.Accounts where objS.AccountID == AccountID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a match if (a != null) { ddlCategory.SelectedIndex = a.CategoryID; //ddlDepartment.SelectedValue= txtAccountName.Text = a.AccountName; if(a.isActive){ ckbIsActive.Checked=true; } } } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store which row was clicked Int32 selectedRow = e.RowIndex; //get the selected StudentID using the grid's Data Key collection Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]); //use EF to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { try { Student s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); //do the delete db.Students.Remove(s); db.SaveChanges(); } catch (Exception) { Response.Redirect("error.aspx"); } } //refresh the grid GetStudents(); }
protected void GetCourses() { // Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { // Course c = (from objS in db.Courses where objS.CourseID == CourseID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a match if (c != null) { ddlDepartment.SelectedIndex = c.DepartmentID; //ddlDepartment.SelectedValue= txtCourseTitle.Text = c.Title; txtCredits.Text = c.Credits.ToString(); } //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent() var objE = (from en in db.Enrollments join s in db.Students on en.StudentID equals s.StudentID where en.CourseID == CourseID select new { en.EnrollmentID, s.LastName, s.FirstMidName, s.EnrollmentDate }); grdStudents.DataSource = objE.ToList(); grdStudents.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Student s = new Student(); int studentID = 0; if (Request.QueryString["StudentID"] != null) { studentID = int.Parse(Request.QueryString["StudentID"]); s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault(); } s.LastName = txtLastName.Text; s.FirstMidName = txtFirstMidName.Text; s.EnrollmentDate = DateTime.Parse(txtEnrollmentDate.Text); if (studentID == 0) { db.Students.Add(s); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("students.aspx"); } }
protected void GetDepartments() { using (comp2007Entities db = new comp2007Entities()) { var departments = from d in db.Departments select d; grdDepartments.DataSource = departments.ToList(); grdDepartments.DataBind(); } }
protected void GetStudents() { using (comp2007Entities db = new comp2007Entities()) { var Students = from s in db.Students select s; grdStudents.DataSource = Students.ToList(); grdStudents.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { // use EF to connect to SQL using (comp2007Entities db = new comp2007Entities()) { //use the student model to save the new model Category c = new Category(); Int32 CategoryID = 0; //check save? or update?? if (Request.QueryString["CategoryID"] != null) { //get the id from the url CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]); //get the current student from EF c = (from objS in db.Categories where objS.CategoryID == CategoryID select objS).FirstOrDefault(); } c.CategoryName = txtCtgname.Text; if (ckbIsExpense.Checked) { c.isExpense = true; } else { c.isExpense = false; } if (ckbActivated.Checked) { c.isActive = true; } else { c.isActive = false; } if (CategoryID == 0) { db.Categories.Add(c); } db.SaveChanges(); //redirect to the updated list page Response.Redirect("category.aspx"); } }
protected void GetCourses() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var Courses = from c in db.Courses select new { c.CourseID, c.Title, c.Credits, c.Department.Name }; grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } }
//show booking list protected void GetBookings() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var Bookings = from b in db.Bookings select new { b.BookingID, b.RoomID, b.LastName, b.FirstName, b.DaysStaying, b.BookDate }; grdBookings.DataSource = Bookings.AsQueryable().OrderBy(SortString).ToList(); grdBookings.DataBind(); } }
protected void GetCourses() { using (comp2007Entities db = new comp2007Entities()) { string SortString = Session["SortColumn"].ToString(); var courses = from c in db.Courses select c; grdCourses.DataSource = courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } }
protected void GetRooms() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var Rooms = from r in db.Rooms select new { r.RoomID, r.Size, r.CostPerDay, r.Availability }; grdRooms.DataSource = Rooms.AsQueryable().OrderBy(SortString).ToList(); grdRooms.DataBind(); } }
protected void GetStudents() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { //query the students table using EF and LINQ var Students = from s in db.Students select s; //bind the result to the gridview grdStudents.DataSource = Students.ToList(); grdStudents.DataBind(); } }
protected void getDepartments() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var departments = from objS in db.Departments select objS; //bind the result to the gridview grdDepartments.DataSource = departments.AsQueryable().OrderBy(SortString).ToList(); grdDepartments.DataBind(); } }
protected void CheckOut() { //get selected room number Int32 RoomID = Convert.ToInt32(Request.QueryString["RoomID"]); using (comp2007Entities db = new comp2007Entities()) { //get selected room Room objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); //populate the room objC.Size = objC.Size; objC.CostPerDay = objC.CostPerDay; objC.Availability = objC.Availability; //set room to available if (objC.Availability == "n") { objC.Availability = "y"; if (String.IsNullOrEmpty(Request.QueryString["RoomID"])) { //add db.Rooms.Add(objC); //save and redirect db.SaveChanges(); } } else { //a customer is already checked into this room Response.Redirect("rooms.aspx"); } Booking objD = (from b in db.Bookings where b.RoomID == RoomID select b).FirstOrDefault(); //remove customer's entry from the bookings db db.Bookings.Remove(objD); db.SaveChanges(); //Redirect Response.Redirect("bookings.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { int selectedRow = e.RowIndex; int courseID = (int)grdCourses.DataKeys[selectedRow].Values["CourseID"]; using (comp2007Entities db = new comp2007Entities()) { Course c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); db.Courses.Remove(c); db.SaveChanges(); GetCourses(); } }
protected void grdCourseStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { int enrollmentID = Convert.ToInt32(grdCourseStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (comp2007Entities db = new comp2007Entities()) { var objE = (from en in db.Enrollments where en.EnrollmentID == enrollmentID select en).FirstOrDefault(); db.Enrollments.Remove(objE); db.SaveChanges(); GetCourse(); } }
protected void GetRoom() { //populate the existing room for editing using (comp2007Entities db = new comp2007Entities()) { Int32 RoomID = Convert.ToInt32(Request.QueryString["RoomID"]); Room objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); //populate the form ddlRoomSize.Text = objC.Size; txtCostPerDay.Text = objC.CostPerDay.ToString(); } }
protected void getMonthly() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); Int32 targetMonth = Convert.ToInt32(ddlMonth.SelectedValue); Int32 targetYear = Convert.ToInt32(ddlYear.SelectedValue); var objE = (from a in db.Accounts select new { a.Category.CategoryName, a.AccountName }); //bind the result to the gridview grdMonthly.DataSource = objE.AsQueryable().OrderBy(SortString).ToList(); grdMonthly.DataBind(); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { int selectedRow = e.RowIndex; int studentID = (int)grdStudents.DataKeys[selectedRow].Values["StudentID"]; using (comp2007Entities db = new comp2007Entities()) { Student s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault(); db.Students.Remove(s); db.SaveChanges(); GetStudents(); } }
protected void GetStudents() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var Students = from s in db.Students select s; //bind the result to the gridview grdStudents.DataSource = Students.AsQueryable().OrderBy(SortString).ToList(); grdStudents.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL Server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Account a = new Account(); Int32 AccountID = 0; //check the querystring for an id so we can determine add / update if (Request.QueryString["AccountID"] != null) { //get the id from the url AccountID = Convert.ToInt32(Request.QueryString["AccountID"]); //get the current student from EF a = (from objS in db.Accounts where objS.AccountID == AccountID select objS).FirstOrDefault(); } a.CategoryID = Convert.ToInt32(ddlCategory.SelectedIndex); a.AccountName = txtAccountName.Text; if (ckbIsActive.Checked) { a.isActive = true; } else { a.isActive = false; } //call add only if we have no student ID if (AccountID == 0) { db.Accounts.Add(a); } //run the update or insert db.SaveChanges(); //redirect to the updated students page Response.Redirect("account.aspx"); } }
protected void getAccounts() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var Accounts = from n in db.Accounts select new { n.AccountID, n.AccountName, n.Category.CategoryName, n.isActive }; //bind the result to the gridview grdAccounts.DataSource = Accounts.AsQueryable().OrderBy(SortString).ToList(); grdAccounts.DataBind(); } }
protected void GetCourses() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var Courses = from c in db.Courses select new { c.CourseID, c.Title, c.Credits, c.Department.Name}; //bind the result to the gridview grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } }
protected void CheckIn() { //populate the existing room for editing using (comp2007Entities db = new comp2007Entities()) { Room objC = new Room(); if (!String.IsNullOrEmpty(Request.QueryString["RoomID"])) { Int32 RoomID = Convert.ToInt32(Request.QueryString["RoomID"]); objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); } //populate the room from the input form objC.Size = objC.Size; objC.CostPerDay = objC.CostPerDay; objC.Availability = objC.Availability; //set availability to no if (objC.Availability == "y") { objC.Availability = "n"; if (String.IsNullOrEmpty(Request.QueryString["RoomID"])) { //add db.Rooms.Add(objC); } //save db.SaveChanges(); } //room not currently available else { //redirect Response.Redirect("rooms.aspx"); } } }
protected void grdRooms_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get selected room number Int32 RoomID = Convert.ToInt32(grdRooms.DataKeys[e.RowIndex].Values["RoomID"]); using (comp2007Entities db = new comp2007Entities()) { //get selected room Room objC = (from r in db.Rooms where r.RoomID == RoomID select r).FirstOrDefault(); //delete db.Rooms.Remove(objC); db.SaveChanges(); //refresh grid GetRooms(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get selected course ID Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); using (comp2007Entities db = new comp2007Entities()) { //get selected course Course objC = (from c in db.Courses where c.CourseID == CourseID select c).FirstOrDefault(); //delete db.Courses.Remove(objC); db.SaveChanges(); //refresh grid GetCourses(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get selected record id Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (comp2007Entities db = new comp2007Entities()) { //get selected record Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); //delete db.Enrollments.Remove(objE); db.SaveChanges(); //refresh the data on the page GetStudent(); } }
protected void GetDepartment() { int departmentID = int.Parse(Request.QueryString["DepartmentID"]); using (comp2007Entities db = new comp2007Entities()) { Department department = (from obj in db.Departments where obj.DepartmentID == departmentID select obj).FirstOrDefault(); txtName.Text = department.Name; txtBudget.Text = department.Budget.ToString(); var objD = (from d in db.Departments join c in db.Courses on d.DepartmentID equals c.DepartmentID where d.DepartmentID == departmentID select new { c.CourseID, c.Title, c.Credits }); grdDepartmentCourses.DataSource = objD.ToList(); grdDepartmentCourses.DataBind(); } }
protected void GetCourses() { using (comp2007Entities db = new comp2007Entities()) { try { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var Courses = from c in db.Courses select new { c.CourseID, c.Title, c.Credits, c.Department.Name }; grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } catch (Exception e) { Response.Redirect("error.aspx"); } } }
protected void GetCourse() { int courseID = int.Parse(Request.QueryString["CourseID"]); using (comp2007Entities db = new comp2007Entities()) { Course course = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); txtTitle.Text = course.Title; txtCredits.Text = course.Credits.ToString(); var objS = (from s in db.Students join e in db.Enrollments on s.StudentID equals e.StudentID join c in db.Courses on e.CourseID equals c.CourseID where c.CourseID == courseID select new { s.StudentID, s.LastName, s.FirstMidName, s.EnrollmentDate, e.EnrollmentID }); grdCourseStudents.DataSource = objS.ToList(); grdCourseStudents.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL Server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Course c = new Course(); Int32 CourseID = 0; //check the querystring for an id so we can determine add / update if (Request.QueryString["CourseID"] != null) { //get the id from the url CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //get the current student from EF c = (from objS in db.Courses where objS.CourseID == CourseID select objS).FirstOrDefault(); } c.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedIndex); c.Title = txtCourseTitle.Text; c.Credits = Convert.ToInt32(txtCredits.Text); //call add only if we have no student ID if (CourseID == 0) { db.Courses.Add(c); } //run the update or insert db.SaveChanges(); //redirect to the updated students page Response.Redirect("courses.aspx"); } }
protected void grdDepartmentCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { int courseID = Convert.ToInt32(grdDepartmentCourses.DataKeys[e.RowIndex].Values["CourseID"]); using (comp2007Entities db = new comp2007Entities()) { var objE = (from en in db.Enrollments where en.CourseID == courseID select en); db.Enrollments.RemoveRange(objE); var objC = (from c in db.Courses where c.CourseID == courseID select c).FirstOrDefault(); db.Courses.Remove(objC); db.SaveChanges(); GetDepartment(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL Server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Student s = new Student(); Int32 StudentID = 0; //check the querystring for an id so we can determine add / update if (Request.QueryString["StudentID"] != null) { //get the id from the url StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //get the current student from EF s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); } s.LastName = txtLastName.Text; s.FirstMidName = txtFirstMidName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text); //call add only if we have no student ID if (StudentID == 0) { db.Students.Add(s); } //run the update or insert db.SaveChanges(); //redirect to the updated students page Response.Redirect("students.aspx"); } }
protected void GetStudent() { int studentID = int.Parse(Request.QueryString["StudentID"]); using (comp2007Entities db = new comp2007Entities()) { Student s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault(); txtLastName.Text = s.LastName; txtFirstMidName.Text = s.FirstMidName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-mm-dd"); var objE = (from en in db.Enrollments join c in db.Courses on en.CourseID equals c.CourseID join d in db.Departments on c.DepartmentID equals d.DepartmentID where en.StudentID == studentID select new { en.EnrollmentID, en.Grade, c.Title, d.Name }); grdStudentCourses.DataSource = objE.ToList(); grdStudentCourses.DataBind(); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store which row was clicked Int32 selectedRow = e.RowIndex; //get the selected StudentID using the grid's Data Key collection Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]); //use EF to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { Student s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); //do the delete db.Students.Remove(s); db.SaveChanges(); } //refresh the grid GetStudents(); }
protected void GetStudents() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { try { //query the students table using EF and LINQ var Students = from s in db.Students select s; //bind the result to the gridview grdStudents.DataSource = Students.ToList(); grdStudents.DataBind(); } catch (Exception) { Response.Redirect("error.aspx"); } } }
protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store which row was clicked Int32 selectedRow = e.RowIndex; //get the selected departmentID using the grid's data key collection Int32 departmentID = Convert.ToInt32(grdDepartments.DataKeys[selectedRow].Values["DepartmentID"]); //use EF to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { Department d = (from objS in db.Departments where objS.DepartmentID == departmentID select objS).FirstOrDefault(); // do the deletion db.Departments.Remove(d); db.SaveChanges(); } //refresh the grid getDepartments(); }