FAQ > Error when student didn’t click the Exit Exam button
Bug description: Null exception error occurred when student didn’t click the Exit Exam button.
Open ResultController in Visual Studio
Go to GetResultViewModel method, replace the code with the following:
[Authorize(Roles = "System Admin, Teacher, Student")]
public ResultViewModel GetResultViewModel(string eId, string sId, StudentExam studentExam, int? totalQuestion = 0)
{
ResultViewModel model = new ResultViewModel();
model.ExamId = eId;
model.StudentId = sId;
var exam = db.Exams.Where(a => a.Id == eId).Select(a => new { MarkToPass = a.MarksToPass, ExamName = a.Name, ReleaseAnswer = a.ReleaseAnswer, StartDate = a.StartDate, Duration = a.Duration }).FirstOrDefault();
decimal? scoreToPass = exam?.MarkToPass;
model.ExamName = exam?.ExamName;
model.ReleaseAnswer = exam?.ReleaseAnswer ?? false;
model.TotalQuestions = (totalQuestion == null || totalQuestion == 0) ? db.ExamQuestions.Where(a => a.ExamId == eId).Count() : totalQuestion;
model.StudentName = db.UserProfiles.Where(a => a.AspNetUserId == sId).Select(a => a.FullName).FirstOrDefault();
model.TotalScore = (from t1 in db.ExamQuestions
where t1.ExamId == eId
select t1.Mark).Sum();
model.AnsweredCorrect = db.StudentAnswerCloneds.Where(a => a.StudentId == sId && a.ExamId == eId && a.MarksObtained > 0).Count();
model.ScoreToPass = scoreToPass;
if (studentExam == null)
{
studentExam = new StudentExam();
}
model.YourScore = studentExam.Result ?? db.StudentAnswerCloneds.Where(a => a.StudentId == sId && a.ExamId == eId && a.MarksObtained > 0).Select(a => a.MarksObtained).Sum();
model.Passed = studentExam.Passed ?? (model.YourScore >= model.ScoreToPass) ? true : false;
model.StartDateTime = studentExam.IsoUtcStartedOn ?? "";
model.EndDateTime = studentExam.IsoUtcEndedOn ?? "";
studentExam.StartedOn = (studentExam.StartedOn == null) ? exam.StartDate : studentExam.StartedOn;
if (studentExam.EndedOn != null && studentExam.StartedOn != null)
{
model.TimeTaken = Math.Round((studentExam.EndedOn - studentExam.StartedOn).Value.TotalMinutes, 2).ToString();
}
else
{
model.TimeTaken = exam.Duration.ToString();
}
return model;
}