public void GridViewQuestions_DeleteItem(int questionId) { using (var context = new PollSystemEntities()) { var question = context.Questions.Include(q => q.Answers).FirstOrDefault(q => q.QuestionId == questionId); if (question == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The question you want to delete was not found!"); return; } try { context.Answers.RemoveRange(question.Answers); context.Questions.Remove(question); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Question successfully deleted!"); } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } this.GridViewQuestions.PageIndex = 0; } }
protected void DeleteAnswer_Command(object sender, CommandEventArgs e) { using (var context = new PollSystemEntities()) { var answerId = Convert.ToInt32(e.CommandArgument); var answer = context.Answers.FirstOrDefault(a => a.AnswerId == answerId); if (answer == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The answer you want to delete was not found!"); return; } try { context.Answers.Remove(answer); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Answer successfully deleted!"); } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } } }
private Question GetQuestion(int questionId) { var context = new PollSystemEntities(); var question = context.Questions.Include(q => q.Answers) .FirstOrDefault(q => q.QuestionId == questionId); return question; }
protected void LinkButtonSaveQuestion_Click(object sender, EventArgs e) { using (PollSystemEntities context = new PollSystemEntities()) { Question question; if (isNewQuestion) { question = new Question(); context.Questions.Add(question); } else { question = context.Questions.Find(this.questionId); } question.QuestionText = this.TextBoxQuestionText.Text; try { context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Question " + (this.isNewQuestion ? "created." : "edited.")); if (isNewQuestion) { ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditQuestion.aspx?questionId=" + question.QuestionId, false); } } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
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 Page_PreRender(object sender, EventArgs e) { using (var context = new PollSystemEntities()) { var randomQuestions = context.Questions.Include(q => q.Answers).OrderBy(q => Guid.NewGuid()).Take(3); this.ListViewPolls.DataSource = randomQuestions.ToList(); this.ListViewPolls.DataBind(); } }
protected void GridViewQuestions_SelectedIndexChanged(object sender, EventArgs e) { using (var context = new PollSystemEntities()) { var questionId = Convert.ToInt32(this.GridViewQuestions.SelectedDataKey.Value); var answers = context.Answers.Where(a => a.QuestionId == questionId).ToList(); this.RepeaterAnswers.DataSource = answers; this.RepeaterAnswers.DataBind(); } }
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); } } }
protected void Page_PreRender(object sender, EventArgs e) { if (!isNewAnswer) { using (PollSystemEntities context = new PollSystemEntities()) { Answer answer = context.Answers.Find(answerId); this.TextBoxAnswerText.Text = answer.AnswerText; this.TextBoxVotes.Text = answer.Votes.ToString(); } } }
protected void Page_Load(object sender, EventArgs e) { int questionId = Convert.ToInt32(Request.Params["questionId"]); using (PollSystemEntities context = new PollSystemEntities()) { Question question = context.Questions.Find(questionId); this.LiteralQuestion.Text = question.QuestionText; this.RepeaterAnswers.DataSource = question.Answers.ToList(); this.RepeaterAnswers.DataBind(); } }
protected void Vote_Command(object sender, CommandEventArgs e) { int answerId = Convert.ToInt32(e.CommandArgument); using (PollSystemEntities context = new PollSystemEntities()) { Answer answer = context.Answers.Find(answerId); answer.Votes++; context.SaveChanges(); Response.Redirect("ShowVotingResults.aspx?questionId=" + answer.QuestionId); } }
protected void Page_PreRender(object sender, EventArgs e) { if (!isNewQuestion) { using (PollSystemEntities context = new PollSystemEntities()) { Question question = context.Questions.Find(questionId); this.TextBoxQuestionText.Text = question.QuestionText; this.RepeaterAnswers.DataSource = question.Answers.ToList(); this.RepeaterAnswers.DataBind(); this.LinkButtonCreateNewAnswer.Visible = true; } } }
protected void Delete_Command(object sender, CommandEventArgs e) { int answerId = Convert.ToInt32(e.CommandArgument); PollSystemEntities context = new PollSystemEntities(); try { context.Database.ExecuteSqlCommand( "DELETE FROM Answers WHERE AnswerId={0}", answerId); ErrorSuccessNotifier.AddInfoMessage("Question successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public void GridViewQuestions_DeleteItem(int questionId) { PollSystemEntities context = new PollSystemEntities(); Question question = context.Questions.Include("Answers"). FirstOrDefault(q => q.QuestionId == questionId); try { context.Answers.RemoveRange(question.Answers); context.Questions.Remove(question); context.SaveChanges(); this.GridViewQuestions.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("Question successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void Page_PreRender(object sender, EventArgs e) { if (this.answerId != 0) { using (var context = new PollSystemEntities()) { var answer = context.Answers.FirstOrDefault(a => a.AnswerId == answerId); if (answer == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The answer you want to edit was not found!"); return; } this.TextBoxAnswerText.Text = answer.AnswerText; this.TextBoxAnswerVotes.Text = answer.Votes.ToString(); } } }
protected void ButtonSaveQuestionText_Click(object sender, EventArgs e) { using (var context = new PollSystemEntities()) { Question question = null; if (this.questionId == 0) // Creating a new question { question = new Question(); context.Questions.Add(question); } else // Editing an existing question { question = context.Questions.FirstOrDefault(q => q.QuestionId == this.questionId); if (question == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The question you want to edit was not found!"); return; } } try { question.QuestionText = this.TextBoxQuestionText.Text; context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Question successfully " + (this.questionId == 0 ? "created!" : "edited!")); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("InsertEditQuestions.aspx", false); } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); return; } } }
protected void Vote_Command(object sender, CommandEventArgs e) { using (var context = new PollSystemEntities()) { var answerId = Convert.ToInt32(e.CommandArgument); var answer = context.Answers.FirstOrDefault(a => a.AnswerId == answerId); if (answer == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The question or the answer you have voted for was not found!"); } else { answer.Votes++; context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("You have voted successfully!"); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("VotingResults.aspx?questionId=" + answer.QuestionId); } } }
public IQueryable<Question> GridViewQuestions_GetData() { PollSystemEntities context = new PollSystemEntities(); return context.Questions.OrderBy(q => q.QuestionId); }
protected void Page_PreRender(object sender, EventArgs e) { if (this.questionId != 0) { using (var context = new PollSystemEntities()) { var question = context.Questions.Include(q => q.Answers).FirstOrDefault(q => q.QuestionId == this.questionId); if (question == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! The question you want to edit was not found!"); return; } this.TextBoxQuestionText.Text = question.QuestionText; this.RepeaterAnswers.DataSource = question.Answers; this.RepeaterAnswers.DataBind(); this.LinkButtonCreateNewAnswer.Visible = true; } } }