public StudentInCourse GetOrCreateStudentInCourse(int studentId, int courseId) { try { var studentInCourse = _cycodeContext.StudentInCourses.SingleOrDefault(sic => sic.CourseId == courseId && sic.StudentId == studentId); if (studentInCourse == null) { studentInCourse = new StudentInCourse { StudentId = studentId, CourseId = courseId }; _cycodeContext.StudentInCourses.Add(studentInCourse); _cycodeContext.SaveChanges(); } return(studentInCourse); } catch (DbException e) { throw new DALException("There was a problem getting or creating student in course", e); } }
private async Task <List <int> > RemoveOrFlagUsers(CourseReconcile courseToReconcile, IEnumerable <string> bbIdsToRemove) { var idsRemoved = new List <int>(); foreach (var studReconcile in courseToReconcile.StudentsToReconcile.Where(str => bbIdsToRemove.Contains(str.BbUserId))) { idsRemoved.Add(studReconcile.PersonId); var sic = new StudentInCourse { StudentPersonId = studReconcile.PersonId, CourseId = courseToReconcile.Course.Id }; if (studReconcile.CanDelete) { ctxManager.Context.Entry(sic).State = System.Data.Entity.EntityState.Deleted; } else { sic.DeletedDate = DateTime.Now; sic.DeletedById = Faculty.PersonId; sic.IsDeleted = true; ctxManager.Context.Entry(sic).State = System.Data.Entity.EntityState.Modified; } } foreach (var facReconcile in courseToReconcile.FacultyToReconcile.Where(str => bbIdsToRemove.Contains(str.BbUserId))) { idsRemoved.Add(facReconcile.PersonId); var fic = new FacultyInCourse { FacultyPersonId = facReconcile.PersonId, CourseId = courseToReconcile.Course.Id }; if (facReconcile.CanDelete) { ctxManager.Context.Entry(fic).State = System.Data.Entity.EntityState.Deleted; } else { fic.DeletedDate = DateTime.Now; fic.DeletedById = Faculty.PersonId; fic.IsDeleted = true; ctxManager.Context.Entry(fic).State = System.Data.Entity.EntityState.Modified; } } await ctxManager.Context.SaveChangesAsync(); return(idsRemoved); }
protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { CheckBox cb = (CheckBox)sender; GridViewRow gvr = (GridViewRow)cb.NamingContainer; Label1.Text = ""; int CourseID = Int32.Parse(gvr.Cells[0].Text); int StudentID = (int)Session["StudentID"]; if (cb.Checked) { //Array[7][9] bool[][] AllottedTable = Classes.HogwartsDataAccess.GetStudentAllotedTable((int)Session["SemesterID"], StudentID); List <Session> ls = Classes.HogwartsDataAccess.GetSessions(CourseID); foreach (Session s in ls) { if (AllottedTable[s.Weekday][s.StartingPeriod] || AllottedTable[s.Weekday][s.StartingPeriod + 1]) { Label1.Text = "School time is ovelapped"; cb.Checked = false; return; } } HogwartsDatabaseModelDataContext db = new HogwartsDatabaseModelDataContext(); var course = (from c in db.Courses where c.CourseID == CourseID select c).First(); if (course.AttendeeNumber >= course.MaxCapacity) { Label1.Text = "Course is full"; return; } StudentInCourse sic = new StudentInCourse(); sic.CourseID = CourseID; sic.StudentID = StudentID; db.StudentInCourses.InsertOnSubmit(sic); if (course.AttendeeNumber == null) { course.AttendeeNumber = 1; gvr.Cells[5].Text = "1"; } else { course.AttendeeNumber++; gvr.Cells[5].Text = (Int32.Parse(gvr.Cells[5].Text) + 1).ToString(); } db.SubmitChanges(); db.Connection.Close(); } else { HogwartsDatabaseModelDataContext db = new HogwartsDatabaseModelDataContext(); StudentInCourse sic = (from sc in db.StudentInCourses where sc.CourseID == CourseID && sc.StudentID == StudentID select sc).First(); db.StudentInCourses.DeleteOnSubmit(sic); var course = (from c in db.Courses where c.CourseID == CourseID select c).First(); if (course.AttendeeNumber == 1) { course.AttendeeNumber = null; gvr.Cells[5].Text = ""; } else { course.AttendeeNumber--; gvr.Cells[5].Text = (Int32.Parse(gvr.Cells[5].Text) - 1).ToString(); } db.SubmitChanges(); db.Connection.Close(); } Response.Redirect("AddCourses.aspx"); }
protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { CheckBox cb = (CheckBox)sender; GridViewRow gvr = (GridViewRow)cb.NamingContainer; Label1.Text = ""; int CourseID = Int32.Parse(gvr.Cells[0].Text); int StudentID = (int)Session["StudentID"]; if (cb.Checked) { //Array[7][9] bool[][] AllottedTable = Classes.HogwartsDataAccess.GetStudentAllotedTable((int)Session["SemesterID"], StudentID); List<Session> ls = Classes.HogwartsDataAccess.GetSessions(CourseID); foreach (Session s in ls) { if (AllottedTable[s.Weekday][s.StartingPeriod] || AllottedTable[s.Weekday][s.StartingPeriod + 1]) { Label1.Text = "School time is ovelapped"; cb.Checked = false; return; } } HogwartsDatabaseModelDataContext db = new HogwartsDatabaseModelDataContext(); var course = (from c in db.Courses where c.CourseID == CourseID select c).First(); if (course.AttendeeNumber >= course.MaxCapacity) { Label1.Text = "Course is full"; return; } StudentInCourse sic = new StudentInCourse(); sic.CourseID = CourseID; sic.StudentID = StudentID; db.StudentInCourses.InsertOnSubmit(sic); if (course.AttendeeNumber == null) { course.AttendeeNumber = 1; gvr.Cells[5].Text = "1"; } else { course.AttendeeNumber++; gvr.Cells[5].Text = (Int32.Parse(gvr.Cells[5].Text) + 1).ToString(); } db.SubmitChanges(); db.Connection.Close(); } else { HogwartsDatabaseModelDataContext db = new HogwartsDatabaseModelDataContext(); StudentInCourse sic = (from sc in db.StudentInCourses where sc.CourseID == CourseID && sc.StudentID == StudentID select sc).First(); db.StudentInCourses.DeleteOnSubmit(sic); var course = (from c in db.Courses where c.CourseID == CourseID select c).First(); if (course.AttendeeNumber == 1) { course.AttendeeNumber = null; gvr.Cells[5].Text = ""; } else { course.AttendeeNumber--; gvr.Cells[5].Text = (Int32.Parse(gvr.Cells[5].Text) - 1).ToString(); } db.SubmitChanges(); db.Connection.Close(); } Response.Redirect("AddCourses.aspx"); }