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) { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //instantiate a new student object in memory Ingredient i = new Ingredient(); Measurement m = new Measurement(); Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); //decide if updating or adding, then save if (!String.IsNullOrEmpty(Request.QueryString["ingredient_id"])) { Int32 IngredientID = Convert.ToInt32(Request.QueryString["ingredient_id"]); i = (from ing in conn.Ingredients join mes in conn.Measurements on ing.ingredient_id equals mes.ingredient_id where ing.ingredient_id == IngredientID select ing).FirstOrDefault(); //fill the properties of our object from the form inputs i.Measurements.FirstOrDefault().measurement = txtMeasurement.Text; i.Measurements.FirstOrDefault().unit = ddlUnit.SelectedValue; } i.ingredient_name = txtIngredientName.Text; if (String.IsNullOrEmpty(Request.QueryString["ingredient_id"])) { m.measurement = txtMeasurement.Text; m.unit = ddlUnit.SelectedValue; m.ingredient_id = i.ingredient_id; m.recipe_id = RecipeID; conn.Ingredients.Add(i); conn.Measurements.Add(m); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("~/Admin/recipe.aspx?recipe_id=" + RecipeID); } }
protected void GetRecipe() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the recipe id Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); //get recipe info var r = from rec in conn.Recipes join mes in conn.Measurements on rec.recipe_id equals mes.recipe_id join ing in conn.Ingredients on mes.ingredient_id equals ing.ingredient_id where rec.recipe_id == RecipeID select new { rec.recipe_id, rec.recipe_name, rec.directions, ing.ingredient_id, ing.ingredient_name, mes.measurement, mes.unit }; //populate the form RecipeTitle.Text = r.FirstOrDefault().recipe_name; RecipeDirections.Text = r.FirstOrDefault().directions; grdViewIngredients.DataSource = r.ToList(); grdViewIngredients.DataBind(); } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void GetIngredient() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the ingredient id and the recipe_id Int32 IngredientID = Convert.ToInt32(Request.QueryString["ingredient_id"]); Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); //get recipe info var i = (from rec in conn.Recipes join mes in conn.Measurements on rec.recipe_id equals mes.recipe_id join ing in conn.Ingredients on mes.ingredient_id equals ing.ingredient_id where (rec.recipe_id == RecipeID) && (ing.ingredient_id == IngredientID) select new { ing.ingredient_name, mes.measurement, mes.unit }).FirstOrDefault(); //populate the form txtIngredientName.Text = i.ingredient_name; txtMeasurement.Text = i.measurement; //go through the unit ddl and select the matching unit foreach (ListItem item in ddlUnit.Items) { if (item.Text == i.unit) { ddlUnit.SelectedValue = item.Value; } } } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void GetRecipes() { try { //connect using our connection string from web.config and EF context class using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //use link to query the Recipes model var recipes = from r in conn.Recipes select r; //append the current direction to the sort column String sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdRecipes.DataSource = recipes.AsQueryable().OrderBy(sort).ToList(); grdRecipes.DataBind(); } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
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; } }
protected void grdIngredients_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected ingredient id Int32 IngredientID = Convert.ToInt32(grdIngredients.DataKeys[e.RowIndex].Values["ingredient_id"]); //get the recipe id Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]); try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Measurement objM = (from mes in conn.Measurements where mes.recipe_id == RecipeID && mes.ingredient_id == IngredientID select mes).FirstOrDefault(); conn.Measurements.Remove(objM); conn.SaveChanges(); GetRecipe(); } } catch (Exception exc) { Response.Redirect("~/error.aspx"); } }
protected void grdRecipes_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the selected recipe_id Int32 Recipe_id = Convert.ToInt32(grdRecipes.DataKeys[e.RowIndex].Values["recipe_id"]); var recipe = (from r in conn.Recipes where r.recipe_id == Recipe_id select r).FirstOrDefault(); var measure = (from mes in conn.Measurements where mes.recipe_id == Recipe_id select mes); //delete conn.Measurements.RemoveRange(measure); conn.Recipes.Remove(recipe); conn.SaveChanges(); //update the grid GetRecipes(); } } catch (Exception ex) { Response.Redirect("~/error.aspx"); } }