private void SubmitAnswerToDB(bool isAnswered) { INRMainService myMainService = BusinessStaticInstances.GetSingleMainServiceInstance(); InvestigationAnswerInputDto answer = new InvestigationAnswerInputDto() { QuestionId = _questionObj.Id, Question_Type = _questionObj.Type, QuestionSerialNumber = _questionObj.SerialNumber, Answer_Type = currentAnswerType, InvestigationRecordId = (Guid)App.Current.Properties["CurrentRecordId"] }; if (isAnswered) { answer.AnswerValue1 = currentFrequency; answer.AnswerValue2 = int.Parse(tb_Intake.Text); } else { answer.AnswerValue1 = null; answer.AnswerValue2 = null; } try { myMainService.AddOrUpdateSomeoneInvestigationAnswer(answer); } catch (Exception ex) { throw ex; } }
private void SubmitAnswerToDB() { INRMainService myMainService = BusinessStaticInstances.GetSingleMainServiceInstance(); InvestigationAnswerInputDto answer = new InvestigationAnswerInputDto() { QuestionId = _questionObj.Id, Question_Type = _questionObj.Type, QuestionSerialNumber = _questionObj.SerialNumber, Answer_Type = currentAnswerType, InvestigationRecordId = (Guid)App.Current.Properties["CurrentRecordId"] }; if (currentChoice == 1) { answer.AnswerValue1 = 1; answer.AnswerValue2 = int.Parse(tb_OptionN.Text); } else { answer.AnswerValue1 = 0; answer.AnswerValue2 = null; } try { myMainService.AddOrUpdateSomeoneInvestigationAnswer(answer); } catch (Exception ex) { MessageBox.Show("出现数据异常:" + ex.Message + ",程序返回首页"); ReturnMainPage(); } }
public void AddOrUpdateSomeoneInvestigationAnswer(InvestigationAnswerInputDto answer) { if (answer == null) { throw new ArgumentNullException("答案参数不能为空!"); } using (NutritionalResearchDatabaseEntities mydb = new NutritionalResearchDatabaseEntities()) { var record = mydb.InvestigationRecord.Where(nObj => nObj.Id == answer.InvestigationRecordId).SingleOrDefault(); if (record == null) { throw new ArgumentException("无效的调查记录!"); } if (record.State == (int)InvestigationRecordStateType.FinishedAndAudited) { throw new InvalidOperationException("不能修改已审核的调查记录答案!"); } var _answer = (from nObj in mydb.InvestigationAnswer where nObj.InvestigationRecordId == answer.InvestigationRecordId && nObj.QuestionId == answer.QuestionId select nObj).SingleOrDefault(); if (_answer == null) { InvestigationAnswer _newAnswer = new InvestigationAnswer() { AnswerType = (int)answer.Answer_Type, AnswerValue1 = answer.AnswerValue1, AnswerValue2 = answer.AnswerValue2, CreationTime = DateTime.Now, Id = Guid.NewGuid(), InvestigationRecordId = answer.InvestigationRecordId, QuestionId = answer.QuestionId, QuestionSerialNumber = answer.QuestionSerialNumber, QuestionType = (int)answer.Question_Type }; mydb.InvestigationAnswer.Add(_newAnswer); } else { _answer.AnswerType = (int)answer.Answer_Type; _answer.AnswerValue1 = answer.AnswerValue1; _answer.AnswerValue2 = answer.AnswerValue2; _answer.UpdationTime = DateTime.Now; } try { mydb.SaveChanges(); } catch (Exception ex) { throw ex; } } }