private void NewDeliveryForm_FormClosing(object sender, FormClosingEventArgs e) { if (DialogResult == DialogResult.OK) { //TODO провірки ProvideOrder provideOrder = new ProvideOrder() { Code = CodeGenerator.GenerateCode("ProvideOrder", "po"), Date = DateTime.Now, TotalCost = (int)TotalPrice(), Provide_ID = (int)ProviderComboBox.SelectedValue }; db.ProvideOrder.Add(provideOrder); db.SaveChanges(); provideOrder = (db.ProvideOrder as IEnumerable <ProvideOrder>) .Where(x => x.Code.Equals(provideOrder.Code)) .First(); foreach (Delivery delivery in deliveryContain.Values) { // записуємо вмістиме корзини в бд delivery.Code = CodeGenerator.GenerateCode("Delivery", "d"); delivery.ProvideOrder_ID = provideOrder.ProvideOrder_ID; delivery.Date = DateTime.Now; db.Delivery.Add(delivery); db.SaveChanges(); } //db.SaveChanges(); MessageBox.Show(totalPriceLabel.Text, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } e.Cancel = false; }
public ProvideOrderIEF(ProvideOrder obj, my_db_for_db_2Entities _db) { InitializeComponent(); db = _db; provideBindingSource.DataSource = db.Provide.ToList(); provideOrderBindingSource.DataSource = db.ProvideOrder.ToList(); if (obj == null) { provideOrderBindingSource.DataSource = new ProvideOrder(); db.ProvideOrder.Add(provideOrderBindingSource.Current as ProvideOrder); } else { provideOrderBindingSource.DataSource = obj; db.ProvideOrder.Attach(provideOrderBindingSource.Current as ProvideOrder); } }