Esempio n. 1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            using (RecipesEntities context = new RecipesEntities())
            {
                string categoryName = lbCategory.SelectedItem.ToString();

                Category categoryToBeDeleted = context.Categories.FirstOrDefault(c => c.Name == categoryName);

                DialogResult result = MessageBox.Show("Are you sure want to delete " + categoryName + " category? Every recipe belong to this category will be removed as well.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                // Perform delete category on cascade
                if (result == DialogResult.OK)
                {
                    try
                    {
                        context.Categories.Remove(categoryToBeDeleted);
                        context.SaveChanges();

                        MessageBox.Show("Category has been deleted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        RefreshList();
                    }
                    catch
                    {
                        MessageBox.Show("Unable to delete the category. Category is used by another recipes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            using (RecipesEntities context = new RecipesEntities())
            {
                Recipe_Table recipe = context.Recipe_Tables.FirstOrDefault(r => r.Name == "Chicken Salad");
                //Above line is for searching chicken salad recipe, below line displays
                //it's ID which we know is 1 b/c it's the only recipe in the table
                Console.WriteLine(recipe.Id);



//***********Above is how to select something from your table****

//************Below was how to insert into your table****************
                //Recipe_Table recipe = new Recipe_Table
                //{
                //    Name = "Chicken Salad"
                //};

                //context.Recipe_Tables.Add(recipe);
                //context.SaveChanges(); //Always save changes and refresh


                //Add a new recipe to our recipe table
                //Like adding new object to our recipes collection
                //context.Recipe_Tables

                //***********************************************************
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            using (RecipesEntities context = new RecipesEntities())
            {
                string ingredientName = lbIngredient.SelectedItem.ToString();

                Ingredient ingredientToBeDeleted = context.Ingredients.FirstOrDefault(i => i.Name == ingredientName);

                DialogResult result = MessageBox.Show("Are you sure want to delete " + ingredientName + " ingredient? Every recipe that has this ingredient will be removed as well.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                // Perform delete category on cascade
                if (result == DialogResult.OK)
                {
                    try
                    {
                        context.Ingredients.Remove(ingredientToBeDeleted);
                        context.SaveChanges();

                        MessageBox.Show("Ingredient has been deleted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        RefreshList();
                    }
                    catch
                    {
                        MessageBox.Show("Unable to delete the Ingredients. Ingredients is used in another recipes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }
Esempio n. 4
0
 private void RefreshList()
 {
     using (RecipesEntities context = new RecipesEntities())
     {
         lbCategory.Sorted     = true;
         lbCategory.DataSource = context.Categories.Select(item => item.Name).ToList();
     }
 }
 private void RefreshList()
 {
     using (RecipesEntities context = new RecipesEntities())
     {
         lbIngredient.Sorted     = true;
         lbIngredient.DataSource = context.Ingredients.Select(item => item.Name).ToList();
     }
 }
Esempio n. 6
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            // Add new category
            if (gbCategoryData.Text == "New Data")
            {
                using (RecipesEntities context = new RecipesEntities())
                {
                    // Make sure there is no duplicate category
                    if (context.Categories.FirstOrDefault(c => c.Name == tbCategoryName.Text) == null)
                    {
                        Category newCategory = new Category
                        {
                            Name = tbCategoryName.Text
                        };

                        context.Categories.Add(newCategory);
                        context.SaveChanges();
                        RefreshList();

                        MessageBox.Show("New category has been registered to the system.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Category name has already registered in the system.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            // Update category
            else
            {
                using (RecipesEntities context = new RecipesEntities())
                {
                    Category categoryToBeUpdated = context.Categories.FirstOrDefault(c => c.Name == lbCategory.SelectedItem.ToString());

                    // Make sure there is no duplicate category.
                    if (context.Categories.FirstOrDefault(c => c.Name == tbCategoryName.Text) == null)
                    {
                        categoryToBeUpdated.Name = tbCategoryName.Text;
                        context.SaveChanges();
                        RefreshList();

                        MessageBox.Show("Category has been updated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Category name has already registered in the system.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
Esempio n. 7
0
        private void tbSearch_TextChanged(object sender, EventArgs e)
        {
            using (RecipesEntities context = new RecipesEntities())
            {
                var searchItems = context.Categories.Where(item => item.Name.ToLower().Contains(tbSearch.Text.ToLower())).Select(item => item.Name).ToList();

                lbCategory.DataSource = searchItems.ToList();

                // Reset the items list if there is no search text.
                if (tbSearch.Text == "")
                {
                    RefreshList();
                }
            }
        }