private void btn_Update_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("確認更正?", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { if (tb_RecipeName.Text == null) { MessageBox.Show("未填寫食譜名稱", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (pb_RecipeIMG.Image == null) //判斷是否未上傳圖片 { MessageBox.Show("食譜圖片不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_Howmany.Text == "" || tb_Time.Text == "") //判斷是否為空 { MessageBox.Show("人數及時間欄位不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_RecipeDescription.Text == "") { MessageBox.Show("食譜描述不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (tb_Video.Text != "" && (!tb_Video.Text.Contains("youtu.be") && !tb_Video.Text.Contains("youtube"))) //判斷是否為Youtube網址 { MessageBox.Show("非YouTube網址", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (ID_List.Count == 0) { MessageBox.Show("食譜食材不得為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Step_List.Count == 0) { MessageBox.Show("步驟不可為空", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } else //全部確認完成 { MemoryStream memory = new MemoryStream(); pb_RecipeIMG.Image.Save(memory, ImageFormat.Png); RecipeByte = memory.GetBuffer(); var Update = Delicious.Recipe_Table.AsEnumerable().Where(A => A.RecipeID == NowID).Select(A => A); foreach (var item in Update) { item.Picture = RecipeByte; item.RecipeName = tb_RecipeName.Text; item.RecipeDescription = tb_RecipeDescription.Text; item.RecipeCategoryID = cb_RecipeCategory.SelectedIndex + 1; item.ForHowMany = Convert.ToInt32(tb_Howmany.Text); item.TimeNeed = Convert.ToInt32(tb_Time.Text); item.Video = tb_Video.Text; } Delicious.SaveChanges(); var Update2 = Delicious.Ingredient_Record_Table.AsEnumerable().Where(B => B.RecipeID == NowID); Delicious.Ingredient_Record_Table.RemoveRange(Update2); Delicious.SaveChanges(); for (int i = 0; i < ID_List.Count; i++) { var Ingredient = new Ingredient_Record_Table() { IngredientID = ID_List[i], RecipeID = NowID, Unt = Unt_List[i], IngredientAmountForUse = ForUse_List[i], }; Delicious.Ingredient_Record_Table.Add(Ingredient); } Delicious.SaveChanges(); var Update3 = Delicious.Steps_Table.AsEnumerable().Where(C => C.RecipeID == NowID); Delicious.Steps_Table.RemoveRange(Update3); Delicious.SaveChanges(); for (int i = 0; i < Step_List.Count; i++) { var AddStep = new Steps_Table() { RecipeID = NowID, StepsNumber = i + 1, Steps = Step_List[i], Picture = StepImg_List[i] }; Delicious.Steps_Table.Add(AddStep); } Delicious.SaveChanges(); MessageBox.Show("修改成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } } }
private void btn_Save_Click(object sender, EventArgs e) { try { DialogResult result = MessageBox.Show("確認新增?", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { var Recipe = new Recipe_Table() { MemberID = Viewbag.member.MemberID, Picture = RecipeByte, RecipeName = tb_RecipeName.Text, RecipeCategoryID = cb_CategoryName.SelectedIndex + 1, ForHowMany = Convert.ToInt32(tb_howmany.Text), TimeNeed = Convert.ToInt32(tb_Time.Text) * 60, RecipeDescription = tb_RecipeDescription.Text, Video = tb_Video.Text, PostTime = DateTime.Now, ShareLink = "https://www.youtube.com/watch?v=072tU1tamd0", DeleteOrNot = false, DisVisible = false }; Delicious.Recipe_Table.Add(Recipe); Delicious.SaveChanges(); var NowRecipeID = (from A in Delicious.Recipe_Table select A.RecipeID).Max(); for (int i = 0; i < ID_List.Count; i++) { var Ingredient = new Ingredient_Record_Table() { IngredientID = ID_List[i], RecipeID = NowRecipeID, Unt = Unt_List[i], IngredientAmountForUse = ForUse_List[i], }; Delicious.Ingredient_Record_Table.Add(Ingredient); } Delicious.SaveChanges(); for (int i = 0; i < Step.Count; i++) { var AddStep = new Steps_Table() { RecipeID = NowRecipeID, StepsNumber = i + 1, Steps = Step[i], Picture = Img[i] }; Delicious.Steps_Table.Add(AddStep); } Delicious.SaveChanges(); MessageBox.Show("新增成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { return; } } catch (Exception) { MessageBox.Show("新增失敗", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } }