public List<StudentResult> GetStudentAllResult(StudentResult aStudentResult)
        {
            try
            {
                connection.Open();
                List<StudentResult> aStudentResults = new List<StudentResult>();
                string query = "SELECT * FROM t_Result WHERE RegistationNo=@regNo";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@regNo", aStudentResult.RegistationNo);
                SqlDataReader ResultReader = command.ExecuteReader();
                while (ResultReader.Read())
                {
                    StudentResult studentResult = new StudentResult();
                    studentResult.RegistationNo = ResultReader[0].ToString();
                    studentResult.DepartmentId = Convert.ToInt16(ResultReader[1].ToString());
                    studentResult.CourseId = Convert.ToInt16(ResultReader[2].ToString());
                    studentResult.GradeLetter = ResultReader[3].ToString();
                    studentResult.Status = Convert.ToInt16(ResultReader[4].ToString());
                    aStudentResults.Add(studentResult);
                }

                return aStudentResults;
            }
               finally
            {
                connection.Close();
            }
        }
 private bool DoesThisSubjectExist(StudentResult aStudentResult)
 {
     List<Course> courses = new List<Course>();
     aStudentManager = new StudentManager();
     courses = aStudentManager.GetStudentCourses(aStudentResult.RegistationNo);
     foreach (Course course in courses)
     {
         if (course.CourseId == aStudentResult.CourseId)
         {
             return true;
         }
     }
     return false;
 }
        public string SaveASubjectGrade(StudentResult aStudentResult)
        {
            aResultGateway = new ResultGateway();

            if (DoesThisSubjectExist(aStudentResult))
                if (DoesThisSubjectResultExist(aStudentResult))
                {
                    if (aStudentResult.GradeLetter == "F")
                        aStudentResult.Status = 1;
                    else
                        aStudentResult.Status = 0;
                    return aResultGateway.SaveASubjectGrade(aStudentResult);
                }

                else
                    return "You already completed this subject";
            else
                return "You Does not Get this Subject";
        }
        protected void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                StudentResult aStudentResult = new StudentResult();
                aStudentResult.DepartmentId = Convert.ToInt16(departmentDropDownList.Text);
                aStudentResult.CourseId = Convert.ToInt16(courseDropDownList.Text);
                aStudentResult.RegistationNo = regNoTextBox.Value;
                aStudentResult.GradeLetter = gradeLetterDropDownList.SelectedItem.Text;
                ResultManager aResultManager = new ResultManager();
                string msg = aResultManager.SaveASubjectGrade(aStudentResult);
                if (msg == "Saved")
                {
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = msg;
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = msg;
                }

            }

            catch (SqlException sqlException)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = "Database error.See details error: " + sqlException.Message;

            }
            catch (Exception exception)
            {
                msgLabel.ForeColor = Color.Red;
                string errorMessage = "Unknow error occured.";
                errorMessage += exception.Message;
                if (exception.InnerException != null)
                {
                    errorMessage += exception.InnerException.Message;
                }
                msgLabel.Text = errorMessage;
            }
        }
        public bool DeletePreviousReselt(StudentResult aStudentResult)
        {
            try
            {
                connection.Open();
                string query = "delete from t_Result Where RegistationNo=@regNo And CourseId=@CourseId";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@regNo", aStudentResult.RegistationNo);
                command.Parameters.AddWithValue("@CourseId", aStudentResult.CourseId);
                command.ExecuteNonQuery();

                return true;
            }

            finally
            {
                connection.Close();
            }
        }
        private bool DoesThisSubjectResultExist(StudentResult aStudentResult)
        {
            List<StudentResult> studentResults = new List<StudentResult>();
            aResultGateway = new ResultGateway();
            studentResults = aResultGateway.GetSubjectResult(aStudentResult);
            if (studentResults.Count == 0)
                return true;
            foreach (StudentResult studentResult in studentResults)
            {
                if (studentResult.CourseId == aStudentResult.CourseId && studentResult.Status == 1)
                {
                    aResultGateway = new ResultGateway();
                    if (aResultGateway.DeletePreviousReselt(aStudentResult))
                        return true;
                }

            }

            return false;
        }
        public string SaveASubjectGrade(StudentResult aStudentResult)
        {
            try
            {
                connection.Open();
                string query = "INSERT INTO t_Result VALUES(@regNo,@deptId,@courseId,@gradeLetter,@status)";
                command.CommandText = query;
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@regNo", aStudentResult.RegistationNo);
                command.Parameters.AddWithValue("@deptId", aStudentResult.DepartmentId);
                command.Parameters.AddWithValue("@courseId", aStudentResult.CourseId);
                command.Parameters.AddWithValue("@gradeLetter", aStudentResult.GradeLetter);
                command.Parameters.AddWithValue("@status", aStudentResult.Status);
                command.ExecuteNonQuery();
                return "Saved";
            }

            finally
            {
                connection.Close();
            }
        }