/// <summary> /// The form is initially created, but loaded each time it is shown. /// So make sure the context is created in the Load event. /// /// This is the handler for the Load event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddOrUpdateCourseForm_Load(object sender, EventArgs e) { this.Tag = null; // Get the context and load the inventory table context = new StudentRegistrationEntities(); context.Database.Log = s => Debug.Write(s); context.SaveChanges(); context.Departments.Load(); context.Courses.Load(); // bind the listbox of Department to the Department table. listBoxCourses.DataSource = context.Courses.Local.ToBindingList(); listBoxDepartment.DataSource = context.Departments.Local.ToBindingList(); // no department is selected to start listBoxCourses.SelectedIndex = -1; listBoxDepartment.SelectedIndex = -1; // set all textboxes to blank textBoxCourseName.ResetText(); textBoxCourseNumber.ResetText(); }
public StudentRegistrationMainForm() { InitializeComponent(); this.Text = "Student Registration using AddUpdate Forms"; context = new StudentRegistrationEntities(); context.Database.Log = (s => Debug.Write(s)); context.SeedDatabase(); context.SaveChanges(); this.Load += (s, e) => StudentRegistratioMainForm_Load(); // set the event handlers for buttons AddOrUpdateStudent addOrUpdateStudentForm = new AddOrUpdateStudent(); buttonStudent.Click += (s, e) => AddOrUpdateForm <Student>(dataGridViewStudent, addOrUpdateStudentForm); AddOrUpdateDepartment addOrUpdateDepartmentForm = new AddOrUpdateDepartment(); buttonDepartment.Click += (s, e) => AddOrUpdateForm <Department>(dataGridViewDepartment, addOrUpdateDepartmentForm); AddOrUpdateCourse addOrUpdateCourseForm = new AddOrUpdateCourse(); buttonCourse.Click += (s, e) => AddOrUpdateForm <Course>(dataGridViewCourse, addOrUpdateCourseForm); buttonRegister.Click += ButtonRegister_Click; buttonDrop.Click += ButtonDrop_Click; }
/// <summary> /// Remove selected Row /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonDrop_Click(object sender, EventArgs e) { /* foreach (DataGridViewRow row in dataGridViewRegistration.SelectedRows) * { * dataGridViewRegistration.Rows.RemoveAt(row.Index); * }*/ StudentRegistrationEntities context = new StudentRegistrationEntities(); context.Students.Load(); context.Courses.Load(); context.Database.Log = (s => Debug.Write(s)); context.SaveChanges(); // get the students and courses, and include nav properties var students = context.Students.Include("Courses").ToList(); var courses = context.Courses.Include("Students").ToList(); foreach (DataGridViewRow row in dataGridViewRegistration.SelectedRows) { StudentCourseRegistration registration = row.DataBoundItem as StudentCourseRegistration; // find the student in the db Student student = students.Find(s => s.StudentId == registration.StudentID); // find the course in teh db Course course = courses.Find(c => c.CourseNumber == registration.CourseNumber && c.DepartmentId == registration.course.DepartmentId); student.Courses.Remove(course); } context.SaveChanges(); context.Dispose(); UpdateRegistration(); }
protected void Page_Load(object sender, EventArgs e) { base.Page_Load(sender, e); if (!Page.IsPostBack) { drpSemester.AppendDataBoundItems = true; drpSemester.DataSource = Semester.ToList(); drpYears.AppendDataBoundItems = true; drpYears.DataSource = Years.ToList(); using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { var cs = (from c in entityContext.Courses orderby c.CourseTitle select new { CourseId = c.CourseID, CourseText = c.CourseID + " - " + c.CourseTitle }).ToList(); if (cs.Count() == 0) { Response.Redirect("AddCourse.aspx"); } else { drpCourse.DataSource = cs; drpCourse.DataTextField = "CourseText"; drpCourse.DataValueField = "CourseID"; drpCourse.AppendDataBoundItems = true; } } } }
/// <summary> /// Method to display all registrations /// </summary> /// <param name="datagridviewRegistrations"> datagridview to be used for display</param> private static void InitializeRegistrationView(DataGridView datagridviewRegistrations) { // set number of columns datagridviewRegistrations.ColumnCount = 5; // Set the column header names. datagridviewRegistrations.Columns[0].Name = "Department"; datagridviewRegistrations.Columns[1].Name = "Course Number"; datagridviewRegistrations.Columns[2].Name = "Course Name"; datagridviewRegistrations.Columns[3].Name = "Student ID"; datagridviewRegistrations.Columns[4].Name = "Last Name"; // using unit-of-work context using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { // loop through all courses foreach (Course course in context.Courses) { // loop over all courses for each student foreach (Student st in course.Students) { // get the needed information string[] rowAdd = { course.Department.DepartmentCode, course.CourseNumber.ToString(), course.CourseName, st.StudentId.ToString(), st.StudentLastName }; // add to display datagridviewRegistrations.Rows.Add(rowAdd); } } } // set all properties datagridviewRegistrations.AllowUserToAddRows = false; datagridviewRegistrations.AllowUserToDeleteRows = false; datagridviewRegistrations.ReadOnly = true; datagridviewRegistrations.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; }
protected void Page_Load(object sender, EventArgs e) { base.Page_Load(sender, e); if (!Page.IsPostBack) { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { var courseOfferingList = (from c in entityContext.CourseOfferings orderby c.Year, c.Semester, c.Course.CourseTitle select new { CourseCode = c.Course_CourseID + ";" + c.Year + ";" + c.Semester, CourseOfferingText = c.Course_CourseID + " " + c.Course.CourseTitle + " " + c.Year + " " + c.Semester }).ToList(); if (courseOfferingList.Count() == 0) { Response.Redirect("AddCourseOffering.aspx"); } else { drpCourseOfferingList.DataSource = courseOfferingList; drpCourseOfferingList.DataTextField = "CourseOfferingText"; drpCourseOfferingList.DataValueField = "CourseCode"; drpCourseOfferingList.AppendDataBoundItems = true; drpCourseOfferingList.DataBind(); } } } }
/// <summary> /// See if a course's id is already taken by another item in course /// </summary> /// <param name="course"></param> /// <returns></returns> public static bool CourseExists(this Course course) { using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { context.Database.Log = (s => Debug.Write(s)); return(context.Courses.Any(c => c.CourseNumber == course.CourseNumber) && context.Courses.Any(c => c.DepartmentId == course.DepartmentId)); } }
/// <summary> /// See if a department's id is already taken by another item in department /// </summary> /// <param name="color"></param> /// <returns></returns> public static bool DepartmentExists(this Department department) { using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { context.Database.Log = (s => Debug.Write(s)); return(context.Departments.Any(c => c.DepartmentId == department.DepartmentId) || context.Departments.Any(c => c.DepartmentCode == department.DepartmentCode)); } }
private StudentRegistrationEntities context; // save DB context here public FormStudentRegistration() { InitializeComponent(); // set up the database context = new StudentRegistrationEntities(); }
protected void btnSubmitCourse_Click(object sender, EventArgs e) { Course course = new Course(txtCourseNumber.Text, txtCourseName.Text, int.Parse(txtWeeklyHours.Text)); using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { entityContext.Courses.Add(course); entityContext.SaveChanges(); } }
private StudentRegistrationEntities context; // save DB context here public StudentRegistrationMainAppForm() { InitializeComponent(); context = new StudentRegistrationEntities(); this.Load += StudentRegistrationMainAppForm_Load; buttonRegister.Click += ButtonRegister_Click; buttonUpdate.Click += ButtonUpdate_Click; }
public void LoadRegistrationView() { using (StudentRegistrationEntities ordersViewContext = new StudentRegistrationEntities()) { ordersViewContext.Database.Log = (s => Debug.Write(s)); // force a read from the db, and we don't need to track these entities dataGridViewRegistration.DataSource = dataGridViewRegistrationContext.Student.AsNoTracking().ToList(); dataGridViewRegistration.Refresh(); } }
/// <summary> /// Load the CustomerOrders view from the db. We need to force a database read, /// so the best way is to create a new context and get all the entities for /// the CustomerOrders view in the db. /// /// </summary> public void LoadStudentRegisterView() { using (StudentRegistrationEntities registrationView = new StudentRegistrationEntities()) { registrationView.Database.Log = (s => Debug.Write(s)); // force a read from the db, and we don't need to track these entities dataGridViewRegistration.DataSource = registrationView.Students.AsNoTracking().ToList(); dataGridViewRegistration.Refresh(); } }
protected void btnSubmitCourseOffering_Click(object sender, EventArgs e) { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { var course = (from c in entityContext.Courses where c.CourseID == drpCourse.SelectedValue select c).FirstOrDefault <Course>(); CourseOffering courseOffering = new CourseOffering(course, int.Parse(drpYears.SelectedValue), drpSemester.SelectedValue); entityContext.CourseOfferings.Add(courseOffering); entityContext.SaveChanges(); } drpCourse.SelectedIndex = drpYears.SelectedIndex = drpSemester.SelectedIndex = 0; }
public AddOrUpdateStudent() { InitializeComponent(); context = new StudentRegistrationEntities(); // register the event handlers this.Load += AddOrUpdateStudentForm_Load; buttonStudentAdd.Click += ButtonAdd_Click; buttonStudentUpdate.Click += ButtonUpdate_Click; // register event handler for when a car is selected listBoxStudent.SelectedIndexChanged += (s, e) => GetStudent(); }
/// <summary> /// Register selected student and course /// and add to the registration table /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonRegister_Click(object sender, EventArgs e) { StudentRegistrationEntities context = new StudentRegistrationEntities(); context.Students.Load(); context.Courses.Load(); context.Database.Log = (s => Debug.Write(s)); context.SaveChanges(); var students = context.Students.Include("Courses").ToList(); var courses = context.Courses.Include("Students").ToList(); // check if bothe student and course are selected if (dataGridViewStudent.SelectedRows.Count == 0 || dataGridViewCourse.SelectedRows.Count == 0) { MessageBox.Show("No students or Courses selected"); return; } // get the selected students and keep in a list List <Student> studentsToRegister = new List <Student>(); foreach (DataGridViewRow row in dataGridViewStudent.SelectedRows) { Student student = row.DataBoundItem as Student; studentsToRegister.Add(students.Find(s => s.StudentId == student.StudentId)); } List <Course> courseToRegister = new List <Course>(); foreach (DataGridViewRow row in dataGridViewCourse.SelectedRows) { Course course = row.DataBoundItem as Course; courseToRegister.Add(courses.Find(c => c.CourseId == course.CourseId && c.DepartmentId == course.DepartmentId)); } foreach (Course c in courseToRegister) { foreach (Student s in studentsToRegister) { c.Students.Add(s); } } context.SaveChanges(); UpdateRegistration(); context.Dispose(); }
/// <summary> /// Method to load all datagridviews and seed the initial database /// </summary> private void StudentRegistrationAppMainForm_Load() { // using the Unit-of-work context // seed the database using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { context.SeedDatabase(); } // common setup for datagridview controls InitializeDataGridView <Student>(dataGridViewStudents, "Courses", "Department"); InitializeDataGridView <Course>(dataGridViewCourses, "Students", "Department"); InitializeDataGridView <Department>(dataGridViewDepartments, "Courses", "Students"); // set up the registration view using a custom method InitializeRegistrationView(dataGridViewRegistrations); }
private StudentRegistrationEntities context; // save DB context here public FormStudentRegistration() { InitializeComponent(); // set up the database context = new StudentRegistrationEntities(); // set up database log to write to output window in VS context.Database.Log = s => Debug.Write(s); context.SaveChanges(); // delete db if exists, then create context.Database.Delete(); context.Database.Create(); }
protected CourseOffering GetCourseOfferingFromDropdown(StudentRegistrationEntities entityContext) { string[] details = drpCourseOfferingList.SelectedValue.Split(';'); if (details.Count() < 3) { return(new CourseOffering()); } string courseID = details[0]; int year = int.Parse(details[1]); string semester = details[2]; var cos = (from co in entityContext.CourseOfferings where co.Course.CourseID == courseID && co.Year == year && co.Semester == semester select co).FirstOrDefault <CourseOffering>(); return(cos); }
protected void Page_PreRender(object sender, EventArgs e) { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { var courseOfferingList = (from c in entityContext.CourseOfferings orderby c.Year, c.Semester, c.Course.CourseTitle select new { CourseCode = c.Course_CourseID, Title = c.Course.CourseTitle, Year = c.Year, Semester = c.Semester }).ToList(); gvAddCourseOffering.DataSource = courseOfferingList; } DataBind(); }
protected void Page_PreRender(object sender, EventArgs e) { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { //List<Course> courseList = entityContext.Courses.ToList<Course>(); var courseList = (from c in entityContext.Courses orderby c.CourseTitle select new { CourseId = c.CourseID, CourseTitle = c.CourseTitle, WeeklyHours = c.HoursPerWeek }).ToList(); gvAddCourse.DataSource = courseList; } DataBind(); }
/// <summary> /// Method to add registration /// </summary> /// <param name="students"> datagridview from which a student was selected</param> /// <param name="courses"> datagridview from which a course was selected</param> private void RegisterStudent(DataGridView students, DataGridView courses) { // if one was not selected- error if (students.SelectedRows.Count == 0 || courses.SelectedRows.Count == 0) { MessageBox.Show("Please Select a Student and a Course for Registration!"); } else { // using unit-of-work context using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { // foreach of the students foreach (DataGridViewRow row in students.SelectedRows) { // check that it is a student - not really required (since mostly display!) // but just in case if (row.DataBoundItem is Student student) { // go through all selected courses foreach (DataGridViewRow rowCourse in courses.SelectedRows) { // check that it is a course - not really required (since mostly display!) // but just in case if (rowCourse.DataBoundItem is Course course) { // get course and student from the context var courseFromContext = context.Courses.Find(course.CourseId); var studentFromContext = context.Students.Find(student.StudentId); // add the course to the student //could've been other way around studentFromContext.Courses.Add(courseFromContext); context.SaveChanges(); // save changes } } } } } } // reload the display of the registrations ReloadRegistrationView(); }
protected void Page_PreRender(object sender, EventArgs e) { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { CourseOffering courseOffering = GetCourseOfferingFromDropdown(entityContext); List <Student> studentList = courseOffering.Students.ToList <Student>(); gvAddStudent.DataSource = studentList; if (drpCourseOfferingList.SelectedIndex == 0) { gvAddStudent.EmptyDataText = "No Course Offering Selected"; } else if (studentList.Count == 0) { gvAddStudent.EmptyDataText = "No Students Registered"; } gvAddStudent.DataBind(); } }
/// <summary> /// Method to reload data in the registration view /// </summary> private void ReloadRegistrationView() { // clear all rows dataGridViewRegistrations.Rows.Clear(); //using unit-of-work displosable context using (StudentRegistrationEntities registrationViewContext = new StudentRegistrationEntities()) { // loop through all courses foreach (Course course in registrationViewContext.Courses) { // loop over all courses for each student foreach (Student st in course.Students) { // get needed data string[] rowAdd = { course.Department.DepartmentCode, course.CourseNumber.ToString(), course.CourseName, st.StudentId.ToString(), st.StudentLastName }; dataGridViewRegistrations.Rows.Add(rowAdd); // add to the datagridview } } } }
public MainForm() { InitializeComponent(); this.Text = "Student Using Add/Update Forms App using EF Code First from DB library"; context = new StudentRegistrationEntities(); context.Database.Log = (s => Debug.Write(s)); context.SeedDatabase(); context.SaveChanges(); this.Load += (s, e) => MainForm_Load(); StudentForm studentForm = new StudentForm(); buttonStudent.Click += (s, e) => AddOrUpdateForm <Student>(dataGridViewStudents, studentForm); DepartmentForm departmentForm = new DepartmentForm(); buttonDepartment.Click += (s, e) => AddOrUpdateForm <Department>(dataGridViewDepartment, departmentForm); }
/// <summary> /// Method to remove all students from the department given the departmentID /// </summary> /// <param name="departmentId"> department id to remove </param> /// <param name="studentsDisplay"> datagridview of students to display changes</param> private static void RemoveStudentsFromDepartment(int departmentId, DataGridView studentsDisplay) { // using unit-of-work context using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { // get the students in that department var students = context.Students.Where(x => x.DepartmentId == departmentId).ToList(); // for all- set their department and departmentId to null foreach (Student st in students) { st.Department = null; st.DepartmentId = null; } // save changes context.SaveChanges(); } // refresh the display to show changes studentsDisplay.DataSource = Controller <StudentRegistrationEntities, Student> .SetBindingList(); studentsDisplay.Refresh(); }
protected void AddStudent_Click(object sender, EventArgs e) { if (drpCourseOfferingList.SelectedValue != "-1") { using (StudentRegistrationEntities entityContext = new StudentRegistrationEntities()) { List <Student> studentList = entityContext.Students.ToList <Student>(); var student = (from c in studentList where c.StudentNum == txtStudentNumber.Text select c).FirstOrDefault <Student>(); if (student == null) { if (rbFullTime.Checked) { student = new FullTimeStudent(txtStudentNumber.Text, txtStudentName.Text); } else if (rbPartTime.Checked) { student = new PartTimeStudent(txtStudentNumber.Text, txtStudentName.Text); } else if (rbCoop.Checked) { student = new CoopStudent(txtStudentNumber.Text, txtStudentName.Text); } entityContext.Students.Add(student); entityContext.SaveChanges(); } CourseOffering courseOffering = GetCourseOfferingFromDropdown(entityContext); List <Student> registeredStudents = courseOffering.Students.ToList <Student>(); if (!registeredStudents.Exists(x => x.Number == student.Number)) { courseOffering.Students.Add(student); entityContext.SaveChanges(); } } } }
/// <summary> /// Method to drop a registration /// </summary> /// <param name="registrations"> datagridview to be processed</param> private void DropFromCourse(DataGridView registrations) { // if none is selected - error! if (registrations.SelectedRows.Count == 0) { MessageBox.Show("Please Select a Registration to be dropped!"); } else { // using unit-of-work context using (StudentRegistrationEntities context = new StudentRegistrationEntities()) { // for every selected row foreach (DataGridViewRow row in registrations.SelectedRows) { // find the student based on ID int selectedStudentID = Convert.ToInt32(row.Cells[3].Value); Student selectedStudent = context.Students.Find(selectedStudentID); // look for Department ID string departmentCode = Convert.ToString(row.Cells[0].Value); Department selectedDepartment = context.Departments.SingleOrDefault(department => department.DepartmentCode == departmentCode); int selectedDepartmentId = selectedDepartment.DepartmentId; // look for Course ID int courseNumber = Convert.ToInt32(row.Cells[1].Value); Course selectedCourse = context.Courses.SingleOrDefault(course => course.CourseNumber == courseNumber && course.DepartmentId == selectedDepartmentId); // remove the course from the student selectedStudent.Courses.Remove(selectedCourse); context.SaveChanges(); // save changes } } // reload the display to show changes ReloadRegistrationView(); } }
public StudentRepository(StudentRegistrationEntities dbContext) { _dbContext = dbContext; }
private StudentRegistrationEntities context; // save DB context here public StudentRegistrationMainAppForm() { InitializeComponent(); context = new StudentRegistrationEntities(); }
public FormStudentRegistration() { InitializeComponent(); context = new StudentRegistrationEntities(); }
public CourseOfferingRepository(StudentRegistrationEntities dbContext) { _dbContext = dbContext; }