// The id parameter name should match the DataKeyNames value set on the control
 public void FormViewQuestion_UpdateItem(int id)
 {
     try
     {
         var context = new ForumEntities();
         ForumApp.Models.Question item = context.Questions.FirstOrDefault(a => a.Id == id);
         // Load the item here, e.g. item = MyDataLayer.Find(id);
         if (item == null)
         {
             // The item wasn't found
             ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
             return;
         }
         TryUpdateModel(item);
         if (ModelState.IsValid)
         {
             // Save changes here, e.g. MyDataLayer.SaveChanges();
             context.Entry(item).State = System.Data.Entity.EntityState.Modified;
             context.SaveChanges();
         }
         ErrorSuccessNotifier.AddInfoMessage("Question successfuly edited!");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage("Server Error: " + ex.Message);
     }
 }
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                ForumEntities context = new ForumEntities();
                var currentCat = context.Categories.Find(Id);
                currentCat.Title = this.CatNameTb.Text;
                context.SaveChanges();

                Response.Redirect("~/Categories.aspx", false);
                Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Successfuly edited");
                Error_Handler_Control.ErrorSuccessNotifier.ShowAfterRedirect = true;
            }
            catch (Exception ex)
            {
                Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Error: " + ex.Message);
            }
        }
        protected void ButtonAddQuestion_Click(object sender, EventArgs e)
        {
            string title = this.TextBoxQuestionTitle.Text;
            int categoryId = Convert.ToInt32(this.DropDownListCategory.SelectedValue);
            string content = this.TextBoxContent.Text;

            if (title.Length < 6)
            {
                ErrorSuccessNotifier.AddErrorMessage("Title length must be at least 6 chars!");
            }
            else if (content.Length < 10)
            {
                ErrorSuccessNotifier.AddErrorMessage("Content length must be at least 10 chars!");
            }
            else
            {
                try
                {
                    ForumEntities context = new ForumEntities();
                    AspNetUser author = context.AspNetUsers.Where(x => x.UserName == User.Identity.Name).FirstOrDefault();

                    ForumApp.Models.Question addedQuestion = new ForumApp.Models.Question()
                    {
                        Title = title,
                        Content = content,
                        CategoryId = categoryId,
                        AuthorId = author.Id,
                        PostedDate = DateTime.Now
                    };

                    context.Questions.Add(addedQuestion);
                    context.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Question added successfuly!");
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    Response.Redirect("~/Question?questionId=" + addedQuestion.Id);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage("Server Error: " + ex.Message);

                }
            }
        }
 protected void LinkButtonAdd_Click(object sender, EventArgs e)
 {
     if (String.IsNullOrEmpty(this.NewCatTextBox.Text))
     {
         ErrorSuccessNotifier.AddErrorMessage("Please enter valid category name!");
     }
     else
     {
         string catName = this.NewCatTextBox.Text;
         var context = new ForumEntities();
         try
         {
             context.Categories.Add(new Models.Category() { Title = catName });
             context.SaveChanges();
             this.NewCatTextBox.Text = null;
             ErrorSuccessNotifier.AddSuccessMessage("Category added successfuly!");
         }
         catch (Exception ex)
         {
             ErrorSuccessNotifier.AddErrorMessage("Server error: " + ex.Message);
         }
     }
 }
        // The id parameter name should match the DataKeyNames value set on the control
        public void ListViewAnswers_UpdateItem(int id)
        {
            var context = new ForumEntities();
            ForumApp.Models.Answer item = context.Answers.FirstOrDefault(a=> a.Id==id);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();

            }
        }
        public void ListViewAnswers_InsertItem()
        {
            var answer = new ForumApp.Models.Answer();
            TryUpdateModel(answer);
            if (ModelState.IsValid)
            {
                try
                {
                    var questionId = int.Parse(Request.Params["questionId"]);
                    var context = new ForumEntities();
                    var question = context.Questions.FirstOrDefault(q => q.Id == questionId);
                    if (question != null)
                    {
                        var user = context.AspNetUsers.FirstOrDefault(u => u.UserName == User.Identity.Name);
                        if (user != null)
                        {
                            answer.AspNetUser = user;
                            answer.PostedDate = DateTime.Now;

                            question.Answers.Add(answer);
                            context.SaveChanges();
                            this.ListViewAnswers.InsertItemPosition = InsertItemPosition.None;
                            ErrorSuccessNotifier.AddSuccessMessage("Ansew added successfuly!");
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Question not found");
                    }
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex.Message);
                }

            }
        }
        // The id parameter name should match the DataKeyNames value set on the control
        public void ListViewAnswers_DeleteItem(int id)
        {
            try
            {
                var questionId = int.Parse(Request.Params["questionId"]);
                var context = new ForumEntities();
                var question = context.Questions.FirstOrDefault(q => q.Id == questionId);
                if (question != null)
                {

                    var answer = context.Answers.FirstOrDefault(a => a.Id == id);
                    if (answer != null)
                    {
                        question.Answers.Remove(answer);
                        context.Answers.Remove(answer);
                        context.SaveChanges();
                        ErrorSuccessNotifier.AddInfoMessage("Answer successfuly deleted");
                    }
                    else
                    {
                        throw new ArgumentException("Answer not found");
                    }
                }
                else
                {
                    throw new ArgumentException("Question not found");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }