private void btnSave_Click(object sender, EventArgs e) { Button oBtn = sender as Button; if (oBtn != null && formloaded) { var cust = (TLADM_CustomerFile)cmboCustomerFile.SelectedItem; if (cust == null) { MessageBox.Show("Please select a customer number"); return; } using (var context = new TTI2Entities()) { var LNU = context.TLADM_LastNumberUsed.Find(1); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString() == bool.FalseString) { continue; } TLSPN_YarnWaste yw = new TLSPN_YarnWaste(); var index = (int)row.Cells[0].Value; yw = context.TLSPN_YarnWaste.Find(index); if (yw != null) { yw.TLYW_Customer_FK = cust.Cust_Pk; yw.TLYW_DateDisposed = dtpSales.Value; yw.TLYW_SalesTransactionNO = LNU.col13; yw.TLYW_Disposed = true; var Dept = context.TLADM_Departments.Where(x => x.Dep_ShortCode.Contains("SPIN")).FirstOrDefault(); if (Dept != null) { var TranType = context.TLADM_TranactionType.Where(x => x.TrxT_Department_FK == Dept.Dep_Id && x.TrxT_Number == 1300).FirstOrDefault(); if (TranType != null) { yw.TLYW_TransactionType_Out = TranType.TrxT_Pk; } } } } try { LNU.col13 += 1; context.SaveChanges(); MessageBox.Show("Data saved successfully to database"); frmViewReport vRep = new frmViewReport(21, LNU.col13 - 1); int h = Screen.PrimaryScreen.WorkingArea.Height; int w = Screen.PrimaryScreen.WorkingArea.Width; vRep.ClientSize = new Size(w, h); vRep.ShowDialog(this); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnSave_Click(object sender, EventArgs e) { TLSPN_YarnWaste YarnW; Button oBtn = sender as Button; bool Add; bool success = false; if (oBtn != null) { using (var context = new TTI2Entities()) { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0] == null || row.Cells[1].Value == null) { continue; } /* * var tst = fieldEntered.Find(x => x.rownumber == row.Index); * if (tst.fieldComplete == null) * continue; * * var cnt = tst.fieldComplete.Where(x => x == false).Count(); * if (cnt != 0) * continue; * */ var Express = row.Cells[1].Value.ToString(); YarnW = context.TLSPN_YarnWaste.Where(x => x.TLYW_BaleNo.Contains(Express)).FirstOrDefault(); if (YarnW != null) { YarnW = context.TLSPN_YarnWaste.Find(YarnW.TLYW_Pk); Add = false; } else { YarnW = new TLSPN_YarnWaste(); Add = true; } var Dept = context.TLADM_Departments.Where(x => x.Dep_ShortCode.Contains("SPIN")).FirstOrDefault(); if (Dept != null) { var TranType = context.TLADM_TranactionType.Where(x => x.TrxT_Department_FK == Dept.Dep_Id && x.TrxT_Number == 1200).FirstOrDefault(); if (TranType != null) { YarnW.TLYW_TransactionType_In = TranType.TrxT_Pk; } } YarnW.TLYW_BaleNo = Express; YarnW.TLYW_BaleGrossWeight = Convert.ToDecimal(row.Cells[2].Value.ToString()); YarnW.TLYW_BaleNettWeight = Convert.ToDecimal(row.Cells[3].Value.ToString()); YarnW.TLYW_Date = dateTimePicker1.Value; try { if (Add) { context.TLSPN_YarnWaste.Add(YarnW); } context.SaveChanges(); success = true; } catch (Exception ex) { MessageBox.Show(ex.Message); success = false; break; } } } if (success) { dataGridView1.Rows.Clear(); MessageBox.Show("Data successfully stored to database"); } } }