private void btnConfirm_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(tbName.Text) || string.IsNullOrWhiteSpace(tbCalories.Text)) { MessageBox.Show("Fields can't be empty!"); return; } using (var fc = new FitLifeDataContent()) { if (_product == null) { _product = new Product() { Name = tbName.Text, Calories = int.Parse(tbCalories.Text) }; fc.Products.Add(_product); } else { if (_product.Name != tbName.Text) { _product.Name = tbName.Text; changed = true; } if (_product.Calories != int.Parse(tbCalories.Text)) { _product.Calories = int.Parse(tbCalories.Text); changed = true; } if (changed) { if (DialogResult.OK == MessageBox.Show("Are you sure?", "Product will be changed!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { fc.Entry(_product).State = System.Data.Entity.EntityState.Modified; } else { this.Close(); } } } fc.SaveChanges(); this.Close(); } }
private void btnConfirm_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(tbName.Text)) { MessageBox.Show("Enter dish Name!"); return; } if (lbIngredients.Items.Count == 0) { MessageBox.Show("Choose ingridients"); return; } string tmpIds = string.Empty; foreach (var item in lbIngredients.Items) { tmpIds += $"{((Product)item).Id} "; } tmpIds = tmpIds.Substring(0, tmpIds.Length - 1); var dish = new Dish() { Name = tbName.Text, Calories = int.Parse(tbCalories.Text), ProductsIds = tmpIds }; using (var fc = new FitLifeDataContent()) { if (_dishInfo != null) { dish.Id = _dishInfo.Id; if (DialogResult.OK == MessageBox.Show("Are you sure?", "Dish will be changed!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) fc.Entry(dish).State = System.Data.Entity.EntityState.Modified; else this.Close(); } else { fc.Dishes.Add(dish); } fc.SaveChanges(); this.Close(); } }