private void btnCopy_Click(object sender, RoutedEventArgs e) { Recipe recipe = new Recipe(); AddRecipeDialog addDialog = new AddRecipeDialog(); if (recipeListBox.SelectedItem != null) { recipe = (Recipe)recipeListBox.SelectedItem; addDialog.titleTextBox.Text = recipe.Title; addDialog.yeildTextBox.Text = recipe.Yield; addDialog.directionTextBox.Text = recipe.Directions; addDialog.servingSizeTextBox.Text = recipe.ServingSize; addDialog.recipeTypeListBox.SelectedValue = recipe.RecipeType; addDialog.commentTextBox.Text = recipe.Comment; bool save = (bool)addDialog.ShowDialog(); if (save) { using (RecipesContext context = new RecipesContext()) { //var query = from title in context.Recipes where title.RecipeID == recipe.RecipeID select title; //foreach (Recipe r in query) //{ recipe.Title = addDialog.titleTextBox.Text; recipe.Yield = addDialog.yeildTextBox.Text; recipe.Directions = addDialog.directionTextBox.Text; recipe.ServingSize = addDialog.servingSizeTextBox.Text; recipe.RecipeType = addDialog.recipeTypeListBox.SelectedValue.ToString(); recipe.Comment = addDialog.commentTextBox.Text; //} try { context.Recipes.Add(recipe); context.SaveChanges(); btnRefresh.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Button.ClickEvent)); } catch { errorLabel.Content = "$Failed to copy {addDialog.titleTextBox.Text}"; } } addDialog.Close(); } else { errorLabel.Content = "You must select a recipe before clicking Modify"; } } }
private void addButton_Click(object sender, RoutedEventArgs e) { try { AddRecipeDialog dialog = new AddRecipeDialog(); if (dialog.ShowDialog() == true) { int newRecipeID = RecipeIDBuilder.GetRecipeID(context.Recipes); Recipe r = RecipeBuilder.BuildRecipe(dialog.titleTextBox.Text, dialog.directionTextBox.Text, dialog.recipeTypeListBox.SelectedValue.ToString(), newRecipeID, dialog.yeildTextBox.Text, dialog.servingSizeTextBox.Text, dialog.commentTextBox.Text); if (string.IsNullOrWhiteSpace(r.Comment)) { r.Comment = null; } if (string.IsNullOrWhiteSpace(r.ServingSize)) { r.ServingSize = null; } if (string.IsNullOrWhiteSpace(r.Yield)) { r.Yield = null; } context.AddNewRecipe(r); recipeListBox.SelectedItem = null; ClearFields(); context.RefreshData(); Recipe[] recipes = getRecipes(); recipeListBox.DataContext = recipes; } } catch (Exception ex) { var baseexception = ex.GetBaseException(); errorLabel.Content = baseexception.Message; } }