private void SaveEditExamCode(object sender, EventArgs e) { try { var examCodeEdited = sender as ExamCodeListViewModel; bool check = false; using (var context = new QLThiTracNghiemDataContext()) { var countUsed = context.ExamTakes.Where(et => et.ExamCodeId == examCodeEdited.ExamCodeId).Count(); if (countUsed <= 0) { var examCodeQuestions = context.ExamCode_Questions.Where(ecq => ecq.ExamCodeId == examCodeEdited.ExamCodeId).ToList(); var examCodeDb = context.ExamCodes.First(ec => ec.ExamCodeId == examCodeEdited.ExamCodeId); examCodeDb.NumberOfQuestions = examCodeEdited.NumberOfQuestions; examCodeDb.SubjectId = examCodeEdited.SubjectId; examCodeDb.GradeId = examCodeEdited.GradeId; examCodeDb.IsPracticeExam = examCodeEdited.IsPracticeExam; context.ExamCode_Questions.DeleteAllOnSubmit(examCodeQuestions); context.SubmitChanges(); List <ExamCode_Question> examCode_Questions = new List <ExamCode_Question>(); foreach (var id in view.EditExamCodeQuestionIds) { var examCode_Question = new ExamCode_Question { ExamCodeId = examCodeEdited.ExamCodeId, QuestionId = id }; examCode_Questions.Add(examCode_Question); } context.ExamCode_Questions.InsertAllOnSubmit(examCode_Questions); context.SubmitChanges(); check = true; } } if (check) { view.SaveEditExamCodeMessage = "Succeed"; } else { view.SaveEditExamCodeMessage = "Used"; } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); view.SaveEditExamCodeMessage = "Failed"; } }
private void AddExamCode(object sender, EventArgs e) { try { var examCodeAdded = sender as ExamCodeListViewModel; using (var context = new QLThiTracNghiemDataContext()) { // Get examCodeId var examCodeIds = context.ExamCodes.Select(ec => ec.ExamCodeId.Substring(2)).ToList(); var examCodeId = examCodeIds.Select(id => int.Parse(id)).Max() + 1; examCodeAdded.ExamCodeId = $"DE{examCodeId:D6}"; var examCode = new ExamCode { ExamCodeId = examCodeAdded.ExamCodeId, NumberOfQuestions = examCodeAdded.NumberOfQuestions, SubjectId = examCodeAdded.SubjectId, GradeId = examCodeAdded.GradeId, IsPracticeExam = examCodeAdded.IsPracticeExam }; context.ExamCodes.InsertOnSubmit(examCode); context.SubmitChanges(); List <ExamCode_Question> examCode_Questions = new List <ExamCode_Question>(); foreach (var id in view.AddExamCodeQuestionIds) { var examCode_Question = new ExamCode_Question { ExamCodeId = examCodeAdded.ExamCodeId, QuestionId = id }; examCode_Questions.Add(examCode_Question); } context.ExamCode_Questions.InsertAllOnSubmit(examCode_Questions); context.SubmitChanges(); } view.AddExamCodeMessage = "Succeed"; } catch (Exception ex) { Debug.WriteLine(ex.ToString()); view.AddExamCodeMessage = "Failed"; } }