Exemplo n.º 1
0
        protected void btn_SubmitCourse_Click(object sender, EventArgs e)
        {
            string courseCode = txtCourseNumber.Text.ToUpper().Trim();
            string action     = Request.Params["action"];

            using (StudentRecordEntities1 entity = new StudentRecordEntities1())
            {
                if (action == "edit")
                {
                    string id     = Request.Params["id"];
                    Course course = (from c in entity.Courses
                                     where c.Code == id
                                     select c).FirstOrDefault <Course>();
                    if (course != null)
                    {
                        course.Title = txtCourseName.Text;
                        entity.Entry(course).State = System.Data.Entity.EntityState.Modified;
                        entity.SaveChanges();
                        Response.Redirect("AddCourse.aspx");
                    }
                }
                else
                {
                    Course course = (from c in entity.Courses
                                     where c.Code == courseCode
                                     select c).FirstOrDefault <Course>();
                    if (course != null)
                    {
                        if (course.Code == courseCode)
                        {
                            err.Text    = "Course with that code already exists!";
                            err.Visible = true;
                        }
                    }
                    else
                    {
                        Course newCourse = new Course();
                        newCourse.Code  = courseCode;
                        newCourse.Title = txtCourseName.Text;

                        entity.Courses.Add(newCourse);
                        entity.SaveChanges();
                        err.Visible = false;
                        Response.Redirect("AddCourse.aspx");
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected void btn_AddToCourse_Click(object sender, EventArgs e)
        {
            string action = Request.Params["action"];

            ActionHandler(action);

            String studentName = txtStudentName.Text.Trim();
            String studentID   = txtStudentNum.Text.Trim();

            String[] courseString = CourseList.SelectedValue.Split();
            string   courseCode   = courseString[0];

            using (StudentRecordEntities1 entityContext = new StudentRecordEntities1())
            {
                // TODO: Add edit actions
                if (action == "edit")
                {
                    string studentId  = Request.Params["id"] as String;
                    string courseId   = Request.Params["course"] as String;
                    Course editCourse = (from c in entityContext.Courses
                                         where c.Code == courseId
                                         select c).FirstOrDefault <Course>();

                    if (editCourse != null)
                    {
                        AcademicRecord record = (from c in editCourse.AcademicRecords
                                                 where c.StudentId == studentId
                                                 select c).FirstOrDefault <AcademicRecord>();

                        record.Grade = int.Parse(txtStudentGrade.Text);
                        entityContext.Entry(record).State = System.Data.Entity.EntityState.Modified;
                        entityContext.SaveChanges();
                        Response.Redirect("AddStudent.aspx");
                    }
                }
                else
                {
                    Course course = (from c in entityContext.Courses
                                     where c.Code == courseCode.Trim()
                                     select c).FirstOrDefault <Course>();

                    string format = course.Code + " " + course.Title;
                    if (CourseList.SelectedValue == format)
                    {
                        int studentGrade = int.Parse(txtStudentGrade.Text.Trim());

                        Student student = (from c in entityContext.Students
                                           where c.Id == studentID
                                           select c).FirstOrDefault <Student>();

                        if (student != null)
                        {
                            AcademicRecord record = (from c in student.AcademicRecords
                                                     where c.CourseCode == course.Code
                                                     select c).FirstOrDefault <AcademicRecord>();

                            if (course.AcademicRecords.Contains(record))
                            {
                                errMsg.Text    = "The system already has a record of this student for the selected course";
                                errMsg.Visible = true;
                            }
                            else
                            {
                                AcademicRecord newRecord = new AcademicRecord();
                                newRecord.Grade   = studentGrade;
                                newRecord.Student = student;
                                newRecord.Course  = course;
                                course.AcademicRecords.Add(newRecord);
                                entityContext.SaveChanges();
                                Response.Redirect("AddStudent.aspx");
                            }
                        }
                        else
                        {
                            Student newStudent = new Student();
                            newStudent.Name = studentName;
                            newStudent.Id   = studentID;
                            AcademicRecord newRecord = new AcademicRecord();
                            newRecord.Grade   = studentGrade;
                            newRecord.Student = newStudent;
                            newRecord.Course  = course;
                            course.AcademicRecords.Add(newRecord);
                            entityContext.SaveChanges();
                            Response.Redirect("AddStudent.aspx");
                        }
                    }
                }
            }
        }