public Double CalculateGPA(int studentId) { var commandForSum = String.Format("SELECT SUM(Total) FROM StudentCourses where StudentId = {0}", studentId); var commandForCountOfSubjects = String.Format("SELECT count(*) FROM StudentCourses where StudentId = {0} GROUP by StudentId", studentId); var sum = _dbaccess.SumOfTotal(commandForSum, connectionString); var countOfSubjects = _dbaccess.CountOfSubjects(commandForCountOfSubjects, connectionString); var total = sum / countOfSubjects; Double GPA = (total - 50) / 10; if (GPA < 0) { GPA = 0; } else if (GPA > 5) { GPA = 5; } var GPACommand = String.Format("Update Student set GPA = {0} where StudentId = {1}", GPA, studentId); _dbaccess.UpdateGPA(GPACommand, connectionString); return(GPA); }