protected void btnAddIngredient_Click(object sender, EventArgs e) { Int32 RecipeID; try { if (!String.IsNullOrEmpty(Request.QueryString["recipe_id"])) { //get the recipe id RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); } else { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //instantiate a new recipe object in memory Recipe r = new Recipe(); //fill the properties of our object from the form inputs r.recipe_name = txtRecipeName.Text; r.directions = txtRecipeDirections.Text; //add the new object and save changes conn.Recipes.Add(r); conn.SaveChanges(); //get recipe info var Recipe = (from rec in conn.Recipes where rec.recipe_name == r.recipe_name && rec.directions == r.directions select new { rec.recipe_id}).FirstOrDefault(); RecipeID = Recipe.recipe_id; } } Response.Redirect("~/Admin/ingredient.aspx?recipe_id=" + RecipeID); } catch (Exception ex) { lblError.Text = ex.Message; } }
protected void btnSave_Click(object sender, EventArgs e) { try { if (String.IsNullOrEmpty(Request.QueryString["recipe_id"])) { lblIngredientMessage.Text = "At Least One Ingredent Must be Added"; lblIngredientMessage.Visible = true; } else { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Recipe r = new Recipe(); Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); r = (from rec in conn.Recipes where rec.recipe_id == RecipeID select rec).FirstOrDefault(); //fill the properties of our object from the form inputs r.recipe_name = txtRecipeName.Text; r.directions = txtRecipeDirections.Text; conn.SaveChanges(); lblIngredientMessage.Visible = false; Response.Redirect("/Admin/recipes.aspx"); } } } catch (Exception exception) { lblError.Text = exception.Message; } }