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 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 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);
                }
            }
        }
        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 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);
                }
            }
        }
Пример #6
0
 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);
     }
 }
 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 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);
                }
            }
        }