public void Init() { using (db = new SurveyDBEntities()) { var ls = db.GetSurveyList(); foreach (GetSurveyList_Result row in ls) { SurveyID = row.SurveyID; SurveyTitle = row.Title; SurveyDescription = row.Description; break; } InitSubjects(this.Subjects); InitAnswers(this.Answers, this.Subjects); } }
public bool CollectAnswers(SurveyVM survey) { using (SurveyDBEntities context = new SurveyDBEntities()) { using (var dbTransaction = context.Database.BeginTransaction()) { try { SurveyResponse response = context.SurveyResponses.Create(); response.SurveyID = survey.SurveyID; response.Company = this.ContactInfo.Company; response.FullName = this.ContactInfo.FullName; response.Email = this.ContactInfo.Email; response.Completed = true; response.LastUpdatedOn = DateTime.Now; context.SurveyResponses.Add(response); context.SaveChanges(); int i = 0; foreach (Subject subject in survey.Subjects) { foreach (Question question in subject.Questions) { Answer answer = this.Answers[i++]; ResponseAnswer responseAnswer = context.ResponseAnswers.Create(); responseAnswer.ResponseID = response.ResponseID; responseAnswer.QuestionID = question.QuestionID; responseAnswer.SelectedAnswer = (int)answer.SelectedAnswer; responseAnswer.AnswerText = answer.AnswerText; context.ResponseAnswers.Add(responseAnswer); } } context.SaveChanges(); dbTransaction.Commit(); return(true); } catch (Exception ex) { dbTransaction.Rollback(); //LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Saving to database Error", ex); return(false); } } } }