protected void btnSubmit_Click(object sender, EventArgs e) { if (Page.IsValid) { recipe recipeItem = new recipe(); recipeItem.Name = txtName.Text; recipeItem.SubmittedBy = txtSubmit.Text; recipeItem.PrepTime = Convert.ToInt32(txtTime.Text); recipeItem.Servings = Convert.ToInt32(txtServings.Text); recipeItem.Description = txtDescription.Text; recipeItem.Category = txtCategory.Text; RecipeModel recipeModel = new RecipeModel(); recipeModel.InsertRecipe(recipeItem); int ingamount = Convert.ToInt32(numberIngredients.SelectedValue); IngredientModel ingModel = new IngredientModel(); ingredient ingItem; RecipeIngredientModel recIngModel = new RecipeIngredientModel(); recipeingredient recIngItem; string[] ingredName = { ingredientName1.Text, ingredientName2.Text, ingredientName3.Text, ingredientName4.Text, ingredientName5.Text, ingredientName6.Text, ingredientName7.Text, ingredientName8.Text, ingredientName9.Text, ingredientName10.Text, ingredientName11.Text, ingredientName12.Text, ingredientName13.Text, ingredientName14.Text, ingredientName15.Text }; string[] unitType = { units1.Text, units2.Text, units3.Text, units4.Text, units5.Text, units6.Text, units7.Text, units8.Text, units9.Text, units10.Text, units11.Text, units12.Text, units13.Text, units14.Text, units15.Text }; string[] ingQuant = { ingredientQuantity1.Text, ingredientQuantity2.Text, ingredientQuantity3.Text, ingredientQuantity4.Text, ingredientQuantity5.Text, ingredientQuantity6.Text, ingredientQuantity7.Text, ingredientQuantity8.Text, ingredientQuantity9.Text, ingredientQuantity10.Text, ingredientQuantity11.Text, ingredientQuantity12.Text,ingredientQuantity13.Text, ingredientQuantity14.Text, ingredientQuantity15.Text }; for (int i = 0; i < ingamount; i++) { ingItem = new ingredient(); recIngItem = new recipeingredient(); ingItem.Name = ingredName[i]; ingModel.InsertIngredient(ingItem); recIngItem.Recipe = recipeModel.GetRecipeIDByName(txtName.Text); testLabel.Text = Convert.ToString(recIngItem.Recipe); recIngItem.Ingredient = ingModel.getIngredientIDByName(ingredName[i]); testLabel.Text += Convert.ToString(recIngItem.Ingredient) + " "; recIngItem.UM = unitType[i]; recIngItem.Quantity = Convert.ToInt32(ingQuant[i]); testLabel.Text += recIngItem.UM + " "; testLabel.Text += recIngItem.ToString(); recIngModel.InsertRecipeIngredient(recIngItem); } if (flImage.HasFile) { try { string filename = Path.GetFileName(flImage.FileName); flImage.SaveAs(Server.MapPath("~/") + filename); StatusLabel.Text = "Upload status: File uploaded!"; } catch (Exception ex) { StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; } } } }
protected void btnSave_Click(object sender, EventArgs e) { int RecipeID = Convert.ToInt32(Server.HtmlEncode(Request.Cookies["RecipeID"].Value)); if (txtCategory.Visible == false) { string query = "UPDATE Recipe SET Name='" + txtName.Text + "',Category='" + DropDownList1.SelectedValue.ToString() + "',Description='" + txtDescription.Text + "',PrepTime=" + Convert.ToInt32(txtTime.Text) + ",Servings = " + Convert.ToInt32(this.txtNumOfServing.Text) + " WHERE ID =" + RecipeID + ";"; using (SqlConnection db = new SqlConnection("Data Source=DESKTOP-FOJ2HKK;Initial Catalog=COMP229S17S2;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework")) { SqlCommand command = new SqlCommand(query, db); db.Open(); SqlDataReader reader = command.ExecuteReader(); } } else { string query = "UPDATE Recipe SET Name='" + txtName.Text + "',Category='" + txtCategory.Text + "',Description='" + txtDescription.Text + "',PrepTime=" + Convert.ToInt32(txtTime.Text) + ",Servings = " + Convert.ToInt32(this.txtNumOfServing.Text) + " WHERE ID =" + RecipeID + ";"; using (SqlConnection db = new SqlConnection("Data Source=DESKTOP-FOJ2HKK;Initial Catalog=COMP229S17S2;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework")) { SqlCommand command = new SqlCommand(query, db); db.Open(); SqlDataReader reader = command.ExecuteReader(); } } using (COMP229S17S2Entities db = new COMP229S17S2Entities()) { List <ListOfIngredients> ingredientList = new List <ListOfIngredients>(); foreach (GridViewRow row in grd.Rows) { TextBox ingNameTB = (TextBox)row.FindControl("txtIngredient"); string ingName = ingNameTB.Text; TextBox unitTB = (TextBox)row.FindControl("txtUnit"); string ingUnit = unitTB.Text; TextBox quantityTB = (TextBox)row.FindControl("txtQuantity"); int ingQuantity = Convert.ToInt32(quantityTB.Text); ingredientList.Add(new ListOfIngredients(ingName, ingQuantity, ingUnit)); } db.SaveChanges(); foreach (ListOfIngredients item in ingredientList) { recipeingredient newRepIng = new recipeingredient(); IngredientModel ingModel = new IngredientModel(); RecipeIngredientModel ReInModel = new RecipeIngredientModel(); RecipeModel recipeModel = new RecipeModel(); ingredient newIngredient = new ingredient(); if (ReInModel.CheckIngredient(RecipeID, ingModel.getIngredientIDByName(item.Name)) < 0) { if (ingModel.getIngredientIDByName(item.Name) < 0) { newIngredient.Name = item.Name; ingModel.InsertIngredient(newIngredient); } newRepIng.Ingredient = ingModel.getIngredientIDByName(item.Name); newRepIng.Recipe = RecipeID; newRepIng.Quantity = item.Quantity; newRepIng.UM = item.Unit; ReInModel.InsertRecipeIngredient(newRepIng); } else { string query1 = "UPDATE recipeingredients SET UM='" + item.Unit + "',Quantity=" + item.Quantity + "WHERE Recipe = " + RecipeID + "AND Ingredient = " + ingModel.getIngredientIDByName(item.Name) + ";"; using (SqlConnection db1 = new SqlConnection("Data Source=DESKTOP-FOJ2HKK;Initial Catalog=COMP229S17S2;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework")) { SqlCommand command = new SqlCommand(query1, db1); db1.Open(); SqlDataReader reader = command.ExecuteReader(); } } } List <recipeingredient> freshest = (from recipeIngItems in db.recipeingredients where recipeIngItems.Recipe == RecipeID select recipeIngItems).ToList(); List <int> recipeIngIdsToDelete = new List <int>(); try { for (int i = 0; i < ingredientList.Count; i++) { freshest[i].Quantity = ingredientList[i].Quantity; freshest[i].UM = ingredientList[i].Unit; if (freshest[i].Quantity == 0) { //recipeIngIdsToDelete.Add(freshest[i].id); db.recipeingredients.Remove(freshest[i]); } } } catch (Exception ed) { } db.SaveChanges(); //Redirect bact to the updated recipes page Response.Redirect("Recipes.aspx"); } Label1.Text = TextBox1.Text; }