private void btnSave_Click(object sender, EventArgs e) { Button oBtn = sender as Button; bool Add = false; TLCUT_CutSheetReceipt CSR = null; if (oBtn != null) { if (dataGridView1.Rows.Count < Convert.ToInt32(txtBundles.Text)) { MessageBox.Show("Please complete the form correctly"); return; } var selected = (TLCUT_CutSheet)cmboCutSheet.SelectedItem; if (selected != null) { var Machine = (TLADM_MachineDefinitions)cmboMachines.SelectedItem; if (Machine == null) { MessageBox.Show("Please select a machine on which this cutsheet was cut"); return; } using (var context = new TTI2Entities()) { var Dept = context.TLADM_Departments.Where(x => x.Dep_ShortCode.Contains("CUT")).FirstOrDefault(); var CS = context.TLCUT_CutSheet.Find(selected.TLCutSH_Pk); if (CS != null) { CS.TLCutSH_WIPComplete = true; CS.TLCUTSH_AddNotes = string.Empty; } CSR = context.TLCUT_CutSheetReceipt.Where(x => x.TLCUTSHR_CutSheet_FK == selected.TLCutSH_Pk).FirstOrDefault(); if (CSR == null) { Add = true; CSR = new TLCUT_CutSheetReceipt(); } CSR.TLCUTSHR_Style_FK = selected.TLCutSH_Styles_FK; CSR.TLCUTSHR_CutSheet_FK = selected.TLCutSH_Pk; CSR.TLCUTSHR_Date = dtpTransDate.Value; CSR.TLCUTSHR_NoOfBundles = Convert.ToInt32(txtBundles.Text); CSR.TLCUTSHR_Machine_FK = Machine.MD_Pk; CSR.TLCUTSHR_InBundleStore = true; if (CSR.TLCUTSHR_InBundleStore) { var CutStore = context.TLADM_WhseStore.Where(x => x.WhStore_DepartmentFK == CS.TLCutSH_Department_FK && x.WhStore_BundleStore).FirstOrDefault(); if (CutStore != null) { CSR.TLCUTSHR_WhseBunStore_FK = CutStore.WhStore_Id; CSR.TLCUTSHR_DateIntoBunStore = dtpTransDate.Value; } } if (Add) { context.TLCUT_CutSheetReceipt.Add(CSR); } try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException en) { foreach (var eve in en.EntityValidationErrors) { MessageBox.Show("following validation errors: Type" + eve.Entry.Entity.GetType().Name.ToString() + "State " + eve.Entry.State.ToString()); foreach (var ve in eve.ValidationErrors) { MessageBox.Show("- Property" + ve.PropertyName + " Message " + ve.ErrorMessage); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[2].Value == null) { continue; } TLCUT_CutSheetReceiptDetail CSRD = new TLCUT_CutSheetReceiptDetail(); Add = true; if ((int)row.Cells[0].Value != 0) { var index = (int)row.Cells[0].Value; CSRD = context.TLCUT_CutSheetReceiptDetail.Find(index); if (CSRD != null) { Add = false; } else { CSRD = new TLCUT_CutSheetReceiptDetail(); } } CSRD.TLCUTSHRD_CutSheet_FK = CSR.TLCUTSHR_Pk; CSRD.TLCUTSHRD_Description = (String)row.Cells[1].Value; CSRD.TLCUTSHRD_Size_FK = (int)row.Cells[2].Value; CSRD.TLCUTSHRD_BundleQty = (int)row.Cells[3].Value; CSRD.TLCUTSHRD_InBundleStore = true; if (Dept != null) { var TranType = context.TLADM_TranactionType.Where(x => x.TrxT_Department_FK == Dept.Dep_Id && x.TrxT_Number == 200).FirstOrDefault(); if (TranType != null) { CSRD.TLCUTSHRD_TransactionType = TranType.TrxT_Pk; CSRD.TLCUTSHRD_CurrentStore_FK = (int)TranType.TrxT_ToWhse_FK; } } if (Add) { context.TLCUT_CutSheetReceiptDetail.Add(CSRD); } } TLCUT_CutSheetReceiptBoxes CSRB = new TLCUT_CutSheetReceiptBoxes(); CSRB = context.TLCUT_CutSheetReceiptBoxes.Where(x => x.TLCUTSHB_CutSheet_FK == CSR.TLCUTSHR_Pk).FirstOrDefault(); Add = false; if (CSRB == null) { CSRB = new TLCUT_CutSheetReceiptBoxes(); Add = true; } CSRB.TLCUTSHB_CutSheet_FK = CSR.TLCUTSHR_Pk; if (!String.IsNullOrEmpty(txtAdultBoxes.Text)) { CSRB.TLCUTSHB_AdultBoxes = Convert.ToInt32(txtAdultBoxes.Text); } else { CSRB.TLCUTSHB_AdultBoxes = 0; } if (!String.IsNullOrEmpty(txtKidsBoxes.Text)) { CSRB.TLCUTSHB_KidBoxes = Convert.ToInt32(txtKidsBoxes.Text); } else { CSRB.TLCUTSHB_KidBoxes = 0; } if (!String.IsNullOrEmpty(txtBinding.Text)) { CSRB.TLCUTSHB_Binding = Convert.ToInt32(txtBinding.Text); } else { CSRB.TLCUTSHB_Binding = 0; } if (!String.IsNullOrEmpty(txtRibbing.Text)) { CSRB.TLCUTSHB_Ribbing = Convert.ToInt32(txtRibbing.Text); } else { CSRB.TLCUTSHB_Ribbing = 0; } if (Add) { context.TLCUT_CutSheetReceiptBoxes.Add(CSRB); } try { context.SaveChanges(); MessageBox.Show("Data saved successfully to database"); dataGridView1.Rows.Clear(); dataGridView2.Rows.Clear(); SetUp(); } catch (System.Data.Entity.Validation.DbEntityValidationException en) { foreach (var eve in en.EntityValidationErrors) { MessageBox.Show("following validation errors: Type" + eve.Entry.Entity.GetType().Name.ToString() + "State " + eve.Entry.State.ToString()); foreach (var ve in eve.ValidationErrors) { MessageBox.Show("- Property" + ve.PropertyName + " Message " + ve.ErrorMessage); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }
private void cmboCutSheet_SelectedIndexChanged(object sender, EventArgs e) { ComboBox oCmbo = sender as ComboBox; IList <TLADM_Sizes> sz = new List <TLADM_Sizes>(); int NoOfUnits = 0; if (oCmbo != null && formloaded) { var selected = (TLCUT_CutSheet)oCmbo.SelectedItem; if (selected != null) { dataGridView1.Rows.Clear(); dataGridView2.Rows.Clear(); cutSheet = selected; txtBundles.Enabled = true; txtBundles.Text = "0"; rtbNotes.Text = selected.TLCutSH_Notes; using (var context = new TTI2Entities()) { var Sizes = core.ExtrapNumber(selected.TLCutSH_Size_PN, context.TLADM_Sizes.Count()); Sizes.Sort(); foreach (var Size in Sizes) { var sze = context.TLADM_Sizes.Where(x => x.SI_PowerN == Size).FirstOrDefault(); if (sze != null) { sz.Add(sze); } } var DB = context.TLDYE_DyeBatch.Find(selected.TLCutSH_DyeBatch_FK); if (DB != null) { txtDyeBatch.Text = DB.DYEB_BatchNo; var DO = context.TLDYE_DyeOrder.Find(DB.DYEB_DyeOrder_FK); if (DO != null) { NoOfUnits = context.TLDYE_DyeOrderDetails.Where(x => x.TLDYOD_DyeOrder_Fk == DO.TLDYO_Pk && x.TLDYOD_BodyOrTrim).FirstOrDefault().TLDYOD_Units; txtCustomer.Text = context.TLADM_CustomerFile.Find(DO.TLDYO_Customer_FK).Cust_Description; txtColour.Text = context.TLADM_Colours.Find(cutSheet.TLCutSH_Colour_FK).Col_Description; txtDateOrdered.Text = DO.TLDYO_OrderDate.ToString("dd/MM/yyyy"); DateTime dt = core.FirstDateOfWeek(DO.TLDYO_OrderDate.Year, DO.TLDYO_CutReqWeek); txtDateRequired.Text = dt.AddDays(5).ToString("dd/MM/yyyy"); var DBDetails = context.TLDYE_DyeBatchDetails.Where(x => x.DYEBD_DyeBatch_FK == DB.DYEB_Pk).ToList(); foreach (var row in DBDetails) { if (row.DYEBD_BodyTrim) { txtBody.Text = context.TLADM_Griege.Find(row.DYEBD_QualityKey).TLGreige_Description; } else { if (String.IsNullOrEmpty(txtTrim1.Text)) { txtTrim1.Text = context.TLADM_Griege.Find(row.DYEBD_QualityKey).TLGreige_Description; } else if (String.IsNullOrEmpty(txtTrim2.Text)) { txtTrim2.Text = context.TLADM_Griege.Find(row.DYEBD_QualityKey).TLGreige_Description; } } } var EUnits = (from EUnitsx in context.TLCUT_ExpectedUnits join xSizes in context.TLADM_Sizes on EUnitsx.TLCUTE_Size_FK equals xSizes.SI_id where EUnitsx.TLCUTE_CutSheet_FK == cutSheet.TLCutSH_Pk orderby xSizes.SI_DisplayOrder select EUnitsx).ToList(); // var EUnits = context.TLCUT_ExpectedUnits.Where(x => x.TLCUTE_CutSheet_FK == cutSheet.TLCutSH_Pk).ToList(); foreach (var row in EUnits) { var index = dataGridView2.Rows.Add(); dataGridView2.Rows[index].Cells[0].Value = row.TLCUTE_Size_FK; dataGridView2.Rows[index].Cells[1].Value = context.TLADM_Sizes.Find(row.TLCUTE_Size_FK).SI_Description; dataGridView2.Rows[index].Cells[2].Value = row.TLCUTE_NoofGarments; dataGridView2.Rows[index].Cells[3].Value = 0; dataGridView2.Rows[index].Cells[4].Value = 0.00M; } } } //------------------------------------------------------------ TLCUT_CutSheetReceipt CSR = new TLCUT_CutSheetReceipt(); CSR = context.TLCUT_CutSheetReceipt.Where(x => x.TLCUTSHR_CutSheet_FK == cutSheet.TLCutSH_Pk).FirstOrDefault(); if (CSR != null) { cmboMachines.SelectedValue = CSR.TLCUTSHR_Machine_FK; dtpTransDate.Value = CSR.TLCUTSHR_Date; txtBundles.Text = CSR.TLCUTSHR_NoOfBundles.ToString(); IList <TLCUT_CutSheetReceiptDetail> CSRD = new List <TLCUT_CutSheetReceiptDetail>(); CSRD = context.TLCUT_CutSheetReceiptDetail.Where(x => x.TLCUTSHRD_CutSheet_FK == CSR.TLCUTSHR_Pk).ToList(); if (CSRD != null) { foreach (var row in CSRD) { var index = dataGridView1.Rows.Add(); dataGridView1.Rows[index].Cells[0].Value = row.TLCUTSHRD_Pk; dataGridView1.Rows[index].Cells[1].Value = row.TLCUTSHRD_Description; dataGridView1.Rows[index].Cells[2].Value = row.TLCUTSHRD_Size_FK; dataGridView1.Rows[index].Cells[3].Value = row.TLCUTSHRD_BundleQty; } DataGridViewCellEventArgs exx = new DataGridViewCellEventArgs(3, 1); try { // dataGridView1_CellLeave(dataGridView1, exx); } catch (Exception ex) { MessageBox.Show(ex.Message); } } TLCUT_CutSheetReceiptBoxes CSRB = new TLCUT_CutSheetReceiptBoxes(); CSRB = context.TLCUT_CutSheetReceiptBoxes.Where(x => x.TLCUTSHB_CutSheet_FK == CSR.TLCUTSHR_Pk).FirstOrDefault(); if (CSRB != null) { txtAdultBoxes.Text = CSRB.TLCUTSHB_AdultBoxes.ToString(); txtBinding.Text = CSRB.TLCUTSHB_Binding.ToString(); txtKidsBoxes.Text = CSRB.TLCUTSHB_KidBoxes.ToString(); txtRibbing.Text = CSRB.TLCUTSHB_Ribbing.ToString(); } } } oCmboA.ValueMember = "SI_Id"; oCmboA.DisplayMember = "SI_Description"; oCmboA.DataSource = sz; } } }