protected void LinkButtonSaveQuestion_Click(object sender, EventArgs e)
        {
            using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
            {
                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."));
                    //Response.Redirect("EditQuestions.aspx", false);
                    if (isNewQuestion)
                    {
                        ErrorSuccessNotifier.ShowAfterRedirect = true;
                        Response.Redirect("EditQuestion.aspx?questionId=" + question.QuestionId, false);
                    }
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    return;
                }
            }
        }
        protected void LinkButtonSave_Click(object sender, EventArgs e)
        {
            using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
            {
                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?questionId=" + answer.QuestionId, false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    return;
                }
            }
        }
 protected void Page_PreRender(object sender, EventArgs e)
 {
     int questionId = Convert.ToInt32(Request.Params["questionId"]);
     using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
     {
         Question question = context.Questions.Find(questionId);
         this.LiteralQuestion.Text = question.QuestionText;
         this.RepeaterAnswers.DataSource = question.Answers.ToList();
         this.RepeaterAnswers.DataBind();
     }
 }
 protected void Page_PreRender(object sender, EventArgs e)
 {
     using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
     {
         //var questions = context.Questions.Take(3);
         //this.ListViewPolls.DataSource = questions.ToList();
         var questions = context.Questions.Include("Answers").OrderBy(q => Guid.NewGuid());
         this.ListViewPolls.DataSource = questions.Take(3).ToList();
         this.DataBind();
     }
 }
 protected void Page_PreRender(object sender, EventArgs e)
 {
     if (!isNewAnswer)
     {
         using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
         {
             Answer answer = context.Answers.Find(answerId);
             this.TextBoxAnswerText.Text = answer.AnswerText;
             this.TextBoxVotes.Text = answer.Votes.ToString();
         }
     }
 }
        protected void GridViewQuestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            int questionId = Convert.ToInt32(this.GridViewQuestions.SelectedDataKey.Value);

            using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
            {
                var answers = context.Answers.Where(a => a.QuestionId == questionId).ToList();

                this.RepeaterAnswers.DataSource = answers;
                this.RepeaterAnswers.DataBind();
            }
        }
 protected void Vote_Command(object sender, CommandEventArgs e)
 {
     int answerId = Convert.ToInt32(e.CommandArgument);
     using (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
     {
         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 (PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities())
         {
             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);

            PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities();

            try
            {
                context.Database.ExecuteSqlCommand("DELETE FROM Answers WHERE AnswerId={0}", answerId);
                ErrorSuccessNotifier.AddInfoMessage("Answer successfully deleted.");
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewQuestions_DeleteItem(int questionId)
        {
            PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities();
            Question question = context.Questions.Include("Answers").FirstOrDefault(q => q.QuestionId == questionId);
            if (question == null)
            {
                ErrorSuccessNotifier.AddErrorMessage("Can not delete question: " + 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 GridViewQuestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    this.GridViewQuestions.PageIndex = e.NewPageIndex;
        //}

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<Question> GridViewQuestions_GetData()
        {
            PollSystemIvoCSharpEntities context = new PollSystemIvoCSharpEntities();
            return context.Questions.OrderBy(q => q.QuestionId);
        }