private void btnAdd_Click(object sender, EventArgs e) { if (txtName.Text == "" || txtCalories.Text == "") { MessageBox.Show("請輸入所有欄位"); } else { byte[] bytes; System.IO.MemoryStream ms = new System.IO.MemoryStream(); this.pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bytes = ms.GetBuffer(); detail.Image = bytes; detail.Name = txtName.Text; detail.Calories = Convert.ToInt32(txtCalories.Text); ndto.Fat = float.Parse(txtFat.Text); ndto.Protein = float.Parse(txtProtein.Text); ndto.Carbs = float.Parse(txtCarbs.Text); ndto.Sugar = float.Parse(txtSugar.Text); ndto.VitA = float.Parse(txtVitA.Text); ndto.VitB = float.Parse(txtVitB.Text); ndto.VitC = float.Parse(txtVitC.Text); ndto.VitD = float.Parse(txtVitD.Text); ndto.VitE = float.Parse(txtVitE.Text); ndto.Na = float.Parse(txtNa.Text); ndto.K = float.Parse(txtK.Text); if (IsUpdate) { int mealID = detail.ID; for (int i = 0; i < clbTag.Items.Count; i++) { TagCategoryDetailDTO dto = clbTag.Items[i] as TagCategoryDetailDTO; int categoryID = dto.ID; if (clbTag.GetItemChecked(i)) // Checked. { if (!bll.HasTag(mealID, categoryID)) // Had not been checked. { bll.AddTag(mealID, categoryID); // Check it! } } else // Not checked. { if (bll.HasTag(mealID, categoryID)) // But it WAS checked. { bll.RemoveTag(mealID, categoryID); // Uncheck it. } } } //detail.Tags = bll.GetTagsWithMealID(mealID); bll.Update(detail); MessageBox.Show("已更新餐點資訊"); this.Close(); } else { if (bll.IsMealExist(detail.Name)) { MessageBox.Show("餐點名稱已存在"); } else { detail.NutrientID = nbll.Add(ndto); detail.Image = bytes; detail.Fat = ndto.Fat; detail.Protein = ndto.Protein; detail.Sugar = ndto.Sugar; detail.Carbs = ndto.Carbs; detail.VitA = ndto.VitA; detail.VitB = ndto.VitB; detail.VitC = ndto.VitC; detail.VitD = ndto.VitD; detail.VitE = ndto.VitE; detail.Na = ndto.Na; detail.K = ndto.K; int mealID = bll.Add(detail); for (int i = 0; i < clbTag.Items.Count; i++) { TagCategoryDetailDTO dto = clbTag.Items[i] as TagCategoryDetailDTO; int categoryID = dto.ID; if (clbTag.GetItemChecked(i)) // Checked. { if (!bll.HasTag(mealID, categoryID)) // Had not been checked. { bll.AddTag(mealID, categoryID); // Check it! } } } MessageBox.Show("已新增餐點"); this.Close(); } } } }
private void btnAdd_Click(object sender, EventArgs e) { if (txtName.Text == "" || txtCalories.Text == "") { MessageBox.Show("請輸入所有欄位"); } else { detail.Name = txtName.Text; detail.Calories = Convert.ToInt32(txtCalories.Text); if (IsUpdate) { int mealID = detail.ID; for (int i = 0; i < clbTag.Items.Count; i++) { TagCategoryDetailDTO dto = clbTag.Items[i] as TagCategoryDetailDTO; int categoryID = dto.ID; if (clbTag.GetItemChecked(i)) // Checked. { if (!bll.HasTag(mealID, categoryID)) // Had not been checked. { bll.AddTag(mealID, categoryID); // Check it! } } else // Not checked. { if (bll.HasTag(mealID, categoryID)) // But it WAS checked. { bll.RemoveTag(mealID, categoryID); // Uncheck it. } } } //detail.Tags = bll.GetTagsWithMealID(mealID); bll.Update(detail); MessageBox.Show("已更新餐點資訊"); this.Close(); } else { if (bll.IsMealExist(detail.Name)) { MessageBox.Show("餐點名稱已存在"); } else { int mealID = bll.Add(detail); for (int i = 0; i < clbTag.Items.Count; i++) { TagCategoryDetailDTO dto = clbTag.Items[i] as TagCategoryDetailDTO; int categoryID = dto.ID; if (clbTag.GetItemChecked(i)) // Checked. { if (!bll.HasTag(mealID, categoryID)) // Had not been checked. { bll.AddTag(mealID, categoryID); // Check it! } } } MessageBox.Show("已新增餐點"); this.Close(); } } } }