protected void LinkButtonSave_Click(object sender, EventArgs e) { using (PollSystemEntities context = new PollSystemEntities()) { Answer answer; if (isNewAnswer) { answer = new Answer(); answer.QuestionId = questionId; context.Answers.Add(answer); } else { answer = context.Answers.Find(this.answerId); } try { answer.AnswerText = this.TextBoxAnswerText.Text; answer.Votes = int.Parse(this.TextBoxVotes.Text); context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Answer " + (this.isNewAnswer ? "created." : "edited.")); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditQuestion.aspx?questionId=" + answer.QuestionId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
protected void ButtonSaveAnswer_Click(object sender, EventArgs e) { using (var context = new PollSystemEntities()) { Answer answer = null; if (questionId == 0) // Editing an existing answer { answer = context.Answers.FirstOrDefault(a => a.AnswerId == this.answerId); if (answer == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The answer you want to edit was not found!"); return; } } else // Creating a new answer { answer = new Answer(); var question = context.Questions.FirstOrDefault(q => q.QuestionId == this.questionId); if (question == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The question for the answer you want to create was not found!"); return; } question.Answers.Add(answer); } answer.AnswerText = this.TextBoxAnswerText.Text; var answerVotesString = this.TextBoxAnswerVotes.Text == string.Empty ? "0" : this.TextBoxAnswerVotes.Text; answer.Votes = int.Parse(answerVotesString); if (answer.Votes < 0) { ErrorSuccessNotifier.AddErrorMessage("Invalid votes count! Answer votes cannot be less than 0!"); return; } try { context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Answer successfully " + (this.questionId == 0 ? "edited!" : "created!")); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("InsertEditQuestion.aspx?questionId=" + answer.QuestionId, false); } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } } }