Ejemplo n.º 1
0
        /// <summary>
        /// btnDeleteRecipe click handler, deletes the selected recipe from the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteRecipe_Click(object sender, RoutedEventArgs e)
        {
            if (dgRecipes.SelectedItem != null)
            {
                int    recipeId   = (dgRecipes.SelectedItem as dynamic).RecipeId;
                string recipeName = (dgRecipes.SelectedItem as dynamic).Name;

                MessageBoxResult result = MessageBox.Show($"Are you sure you want to delete recipe '{recipeName}' ?", "Delete Recipe", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.Yes)
                {
                    var recipe = dc.Recipes.Single(r => r.RecipeId == recipeId);
                    dc.RecipeIngredients.DeleteAllOnSubmit(recipe.RecipeIngredients);
                    dc.Recipes.DeleteOnSubmit(recipe);
                    dc.SubmitChanges();
                    refreshRecipies();
                }
            }
            else
            {
                MessageBox.Show("Please select a recipe to delete.", "Delete Recipe");
            }
        }
Ejemplo n.º 2
0
        //Method that  validates entries and saves recipe to Database
        private void btnSaveRecipe_Click(object sender, RoutedEventArgs e)
        {
            string errorMessage = null;

            if (!string.IsNullOrEmpty(txtRecipeName.Text))
            {
                recipe.Name = txtRecipeName.Text;
            }
            else
            {
                errorMessage += "Please write the recipe name.\n";
            }
            if (!string.IsNullOrEmpty(txtRecipeServings.Text))
            {
                recipe.Serving = Convert.ToInt32(txtRecipeServings.Text);
            }

            if (!string.IsNullOrEmpty(txtRecipePrepTime.Text))
            {
                recipe.PrepTime = Convert.ToInt32(txtRecipePrepTime.Text);
            }

            recipe.Description = txtRecipeDescription.Text;

            if (!string.IsNullOrEmpty(txtRecipeDirections.Text))
            {
                recipe.Directions = txtRecipeDirections.Text;
            }
            else
            {
                errorMessage += "Please write the directions.\n";
            }

            if ((int)cbRecipeCategory.SelectedValue != 0)
            {
                recipe.Category = (int)cbRecipeCategory.SelectedValue;
            }
            else
            {
                errorMessage += "Please select a category.\n";
            }
            //if the user added an image to the recipe this will convert it to and array of bytes to be saved in the database table
            if (bitmapImage != null)
            {
                byte[] imageData = new byte[bitmapImage.StreamSource.Length];
                // now, you have get the image bytes array, and you can store it to SQl Server
                bitmapImage.StreamSource.Seek(0, SeekOrigin.Begin);
                //very important, it should be set to the start of the stream
                bitmapImage.StreamSource.Read(imageData, 0, imageData.Length);
                recipe.Image = imageData;
            }
            else
            {
                recipe.Image = null;
            }

            // if there are no errorMessages it will save recipes to database
            if (errorMessage == null)
            {
                //if its a new recipe it will insert a new instance of recipe on submit
                if (recipe.RecipeId == 0)
                {
                    dc.Recipes.InsertOnSubmit(recipe);
                }

                //if the user is editting a recipe and deletes an ingredient we need to first delete the ingredients from the RecipeIngredients
                //table before submiting changes, but we cant delete them from the table before user submits changes to recipe, so we save them
                //in a list and we only delete ingredients from the list if user saves changes to recipe. When submiting changes to a recipe this
                //if checkes if there are any recipe ingredients to be deleted.
                if (recipeIngredientsToDelete.Any())
                {
                    dc.RecipeIngredients.DeleteAllOnSubmit(recipeIngredientsToDelete);
                }

                dc.SubmitChanges();
                Close();
            }
            //if there are any errors during validation a message box will pop up with a list of errors.
            else
            {
                MessageBox.Show(errorMessage, "Error");
            }
        }
Ejemplo n.º 3
0
        //on click of submit button
        private void modifyCategory_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //check if category textbox is empty
                if (string.IsNullOrEmpty(modifyName.Text))
                {
                    modifyName.Text       = "Category name is required";
                    modifyName.Foreground = Brushes.Red;
                }
                else
                {
                    saveRecord(); //function call for assign coming values to caegory object field

                    //Upadte Record if category Id exists
                    if (categoryObj.CategoryId != 0)
                    {
                        //get category name on that edit Id
                        string Cname = (from C in dc.Categories where C.CategoryId == categoryObj.CategoryId select C.CategoryName).Single();
                        // Check if Category name is changed
                        if (modifyName.Text.ToUpper() != Cname)
                        {
                            var category_exist = (from C in dc.Categories where C.CategoryName == modifyName.Text.ToUpper() select C).Count();
                            //if no record exist with enter name then upadte operation going to perform with image and name
                            if (category_exist == 0)
                            {
                                dc.SubmitChanges();
                                MessageBox.Show("Category Record is modified");
                            }
                        }
                        else
                        { //if name exist with enter name then image going to update
                            dc.SubmitChanges();
                            MessageBox.Show("Category Record is modified");
                        }
                    }
                    else
                    {
                        //For Adding new category
                        var category_exist = (from C in dc.Categories where C.CategoryName == modifyName.Text.ToUpper() select C).Count();
                        if (category_exist == 1)
                        {
                            modifyName.Text       = "Category name is alredy exist";
                            modifyName.Foreground = Brushes.Red;
                        }
                        else
                        {
                            dc.Categories.InsertOnSubmit(categoryObj);
                            dc.SubmitChanges();
                            MessageBox.Show("New Category Record " + modifyName.Text.ToUpper() + " Created");
                        }
                        ////after addition and updation category form will open with latest entry
                        //CategoryForm cf = new CategoryForm(categoryObj);
                        //cf.Show();
                        //this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, " error");
            }
        }