protected void btnDelete_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            //Get the row that contains this button
            GridViewRow   gvr             = (GridViewRow)btn.NamingContainer;
            int           coursesubjectid = (int)GridView1.DataKeys[gvr.RowIndex].Values["CourseSubjectId"];
            CourseSubject cs = entities.CourseSubjects.FirstOrDefault(d => d.CourseSubjectId == coursesubjectid);

            if (cs != null)
            {
                try
                {
                    entities.CourseSubjects.Remove(cs);
                    entities.SaveChanges();

                    pnlMessagePanel.Visible = true;
                    lblMessageBox.Text      = "Subject deleted successfully";

                    FetchSubjectCodes();
                }
                catch (Exception excp) {
                    pnlMessagePanel.Visible = true;
                    lblMessageBox.Text      = "Cannot delete the subject " + excp.Message;
                }
            }
        }
        protected void btnAddSubjectToRepeaterDetails_Click(object sender, EventArgs e)
        {
            if (ddlSubjectList.SelectedIndex > 0 && (!(string.IsNullOrEmpty(txtRegisterNumber.Text))))
            {
                pnlMessagePanel.Visible = false;
                StudentRepeaterDetail rd = new StudentRepeaterDetail();

                int           courseSubjectId = int.Parse(ddlSubjectList.SelectedValue);
                CourseSubject cs      = entities.CourseSubjects.FirstOrDefault(d => d.CourseSubjectId == courseSubjectId);
                Student       student = entities.Students.FirstOrDefault(d => d.RegisterNumber == txtRegisterNumber.Text);

                if (cs != null && student != null)
                {
                    if (!entities.StudentRepeaterDetails.Any(d => d.Student.RegisterNumber == txtRegisterNumber.Text && d.CourseSubject.CourseSubjectId == courseSubjectId))
                    {
                        rd.CourseSubject = cs;
                        rd.CreatedDate   = DateTime.Now;
                        rd.CreatedUserId = 1;
                        rd.ExaminationId = 2;
                        rd.Student       = student;

                        entities.StudentRepeaterDetails.Add(rd);
                        entities.SaveChanges();

                        var studentRepeaterList = entities.StudentRepeaterDetails.Where(d => d.Student.RegisterNumber == txtRegisterNumber.Text).Select(d => new
                        {
                            SubjectName = d.CourseSubject.Subject.Name,
                            SubjectCode = d.CourseSubject.Subject.Code,
                            Semester    = d.CourseSubject.Semester
                        }).OrderBy(d => d.Semester).ToList();

                        gvStudentCurrentSubjects.DataSource = studentRepeaterList;
                        gvStudentCurrentSubjects.DataBind();

                        pnlMessagePanel.Visible = true;
                        lblMessageBox.Text      = "Successfully added the Subject";
                    }
                    else
                    {
                        pnlMessagePanel.Visible = true;
                        lblMessageBox.Text      = "The subject already exists for the student";
                    }
                }
                else
                {
                    pnlMessagePanel.Visible = true;
                    lblMessageBox.Text      = "Invalid Student or Course Subject";
                }
            }
            else
            {
                pnlMessagePanel.Visible = true;
                lblMessageBox.Text      = "Please select a valid subject and a valid register number";
            }
        }
        protected void btnAddCourseSubject_Click(object sender, EventArgs e)
        {
            if (ddlSelectDegree.SelectedIndex > 0 &&
                ddlSelectCourse.SelectedIndex > 0 &&
                ddlSelectAcademicYear.SelectedIndex > 0 &&
                ddlSemester.SelectedIndex > 0 &&
                (!string.IsNullOrEmpty(txtSubjectCode.Text)))
            {
                int degreeid     = int.Parse(ddlSelectDegree.SelectedValue);
                int semester     = int.Parse(ddlSemester.SelectedValue);
                int academicyear = int.Parse(ddlSelectAcademicYear.SelectedValue);

                string coursecode  = ddlSelectCourse.SelectedValue;
                string subjectCode = txtSubjectCode.Text;

                Subject subject = entities.Subjects.FirstOrDefault(d => d.Code == subjectCode);

                if (subject != null)
                {
                    Cours c = entities.Courses.First(d => d.Code == coursecode && d.RevisionNo == academicyear);

                    if (c != null)
                    {
                        if (!entities.CourseSubjects.Any(d => d.CourseId == c.CourseId && d.SubjectId == subject.SubjectId))
                        {
                            CourseSubject s = new CourseSubject();
                            s.Cours              = c;
                            s.DegreeConsider     = true;
                            s.HasExamination     = true;
                            s.IsMandatory        = true;
                            s.Semester           = semester;
                            s.Subject            = subject;
                            s.TotalTeachingHours = 0;

                            entities.CourseSubjects.Add(s);
                            entities.SaveChanges();

                            FetchSubjectCodes();

                            pnlMessagePanel.Visible = true;
                            lblMessageBox.Text      = "Subject added successfully";
                        }
                        else
                        {
                            pnlMessagePanel.Visible = true;
                            lblMessageBox.Text      = "Subject already exists for this course";
                        }
                    }
                    else
                    {
                        pnlMessagePanel.Visible = true;
                        lblMessageBox.Text      = "Code does not exist";
                    }
                }
                else
                {
                    pnlMessagePanel.Visible = true;
                    lblMessageBox.Text      = "Subject Code does not exist";
                }
            }
        }