예제 #1
0
        protected void CreateCategoryButton_Click(object sender, EventArgs e)
        {
            if (this.NewCategoryNameBox.Text == string.Empty || this.NewCategoryNameBox.Text.Length > 30)
            {
                this.MessagesTextbox.Text = "Invalid category input data.";
                return;
            }

            var context = new ListDBEntities();
            var newCategory = new Category();
            newCategory.Name = this.NewCategoryNameBox.Text;
            context.Categories.Add(newCategory);
            context.SaveChanges();
            this.DropDownListCategories.DataBind();
            context.Dispose();
        }
예제 #2
0
        protected void DeleteCategoryButton_Click(object sender, EventArgs e)
        {
            var context = new ListDBEntities();
            var idValue = int.Parse(this.DropDownListCategories.SelectedValue);
            var categoryToRemove = context.Categories.SingleOrDefault(c => c.Id == idValue);
            if (categoryToRemove == null)
            {
                this.MessagesTextbox.Text = "Category does not exist.";
                return;
            }

            context.Categories.Remove(categoryToRemove);
            context.SaveChanges();
            this.DropDownListCategories.DataBind();
            context.Dispose();
        }
예제 #3
0
        protected void EditCategoryButton_Click(object sender, EventArgs e)
        {
            if (this.EditCategoryNameBox.Text == string.Empty || this.EditCategoryNameBox.Text.Length > 30)
            {
                this.MessagesTextbox.Text = "Invalid category input data.";
                return;
            }

            var context = new ListDBEntities();
            var idValue = int.Parse(this.DropDownListCategories.SelectedValue);
            var editedCategory = context.Categories.SingleOrDefault(c=> c.Id == idValue);
            if (editedCategory == null)
            {
                this.MessagesTextbox.Text = "Invalid category input data.";
                return;
            }

            editedCategory.Name = this.EditCategoryNameBox.Text;
            context.SaveChanges();
            this.DropDownListCategories.DataBind();
            context.Dispose();
        }