private void btnSave_Click(object sender, EventArgs e) { Button oBtn = sender as Button; bool Add = false; TLADM_QADyeProcess Stages = null; if (oBtn != null && formloaded) { var selected = (TLDYE_NonCompliance)cmboBatches.SelectedItem; if (selected != null) { if (dataGridView4.Rows.Count == 0) { MessageBox.Show("Please supply a non consumable description and quantity"); return; } using (var context = new TTI2Entities()) { if (rbPass.Checked || rbReprocess.Checked) { var DB = context.TLDYE_DyeBatch.Find(selected.TLDYE_NcrBatchNo_FK); if (DB != null) { Stages = (TLADM_QADyeProcess)cmboProcess.SelectedItem; if (rbPass.Checked) { if (Stages.QADYEP_Pk == 1) { DB.DYEB_Stage1 = true; } else if (Stages.QADYEP_Pk == 2) { DB.DYEB_Stage2 = true; } else { DB.DYEB_Stage3 = true; } } if (rbReprocess.Checked) { DB.DYEB_Reprocess = true; if (rbReprocess.Checked) { var Dept = context.TLADM_Departments.Where(x => x.Dep_ShortCode.Contains("DYE")).FirstOrDefault(); if (Dept != null) { TLDYE_DyeTransactions trns = new TLDYE_DyeTransactions(); trns.TLDYET_BatchNo = DB.DYEB_BatchNo; trns.TLDYET_BatchWeight = DB.DYEB_BatchKG; trns.TLDYET_Date = DateTime.Now; trns.TLDYET_SequenceNo = DB.DYEB_SequenceNo; trns.TLDYET_Batch_FK = DB.DYEB_Pk; trns.TLDYET_RejectDate = DateTime.Now; var TT = context.TLADM_TranactionType.Where(x => x.TrxT_Department_FK == Dept.Dep_Id && x.TrxT_Number == 500).FirstOrDefault(); if (TT != null) { trns.TLDYET_TransactionType = TT.TrxT_Pk; trns.TLDYET_CurrentStore_FK = (int)TT.TrxT_ToWhse_FK; } context.TLDYE_DyeTransactions.Add(trns); } } } } } foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString() == bool.FalseString) { continue; } //-------------------------------------------------------------- // We dont want to update the same record twice //---------------------------------------------------------- var Value = (int)row.Cells[0].Value; var record = context.TLDYE_NonComplianceDetail.Where(x => x.DYENCRD_ComNumber == selected.TLDYE_NcrNumber && x.DYENCRD_Code_FK == Value && x.DYENCRD_FR).FirstOrDefault(); if (record != null) { continue; } TLDYE_NonComplianceDetail nonD = new TLDYE_NonComplianceDetail(); nonD.DYENCRD_BatchNo_Fk = selected.TLDYE_NcrBatchNo_FK; nonD.DYENCRD_FR = true; nonD.DYENCRD_Code_FK = (int)row.Cells[0].Value; nonD.DYENCRD_ComNumber = selected.TLDYE_NcrNumber; context.TLDYE_NonComplianceDetail.Add(nonD); } foreach (DataGridViewRow row in dataGridView2.Rows) { if (row.Cells[1].Value.ToString() == bool.FalseString) { continue; } //-------------------------------------------------------------- // We dont want to update the same record twice //---------------------------------------------------------- var Value = (int)row.Cells[0].Value; var record = context.TLDYE_NonComplianceDetail.Where(x => x.DYENCRD_ComNumber == selected.TLDYE_NcrNumber && x.DYENCRD_Code_FK == Value && !x.DYENCRD_FR).FirstOrDefault(); if (record != null) { continue; } TLDYE_NonComplianceDetail nonD = new TLDYE_NonComplianceDetail(); nonD.DYENCRD_BatchNo_Fk = selected.TLDYE_NcrBatchNo_FK; nonD.DYENCRD_FR = false; nonD.DYENCRD_Code_FK = (int)row.Cells[0].Value; nonD.DYENCRD_ComNumber = selected.TLDYE_NcrNumber; context.TLDYE_NonComplianceDetail.Add(nonD); } foreach (DataGridViewRow row in dataGridView3.Rows) { if (row.Cells[1].Value == null) { continue; } var Value = 0.00M; if (row.Cells[4].Value != null) { decimal.TryParse(row.Cells[4].Value.ToString(), out Value); } if (Value == 0.00M) { continue; } Add = false; TLDYE_NonComplianceConsDetail nonD = new TLDYE_NonComplianceConsDetail(); // Cant add the same record twice if (row.Cells[0].Value != null) { var index = (int)row.Cells[0].Value; nonD = context.TLDYE_NonComplianceConsDetail.Find(index); } else { Add = true; } nonD.DYENCCON_BatchNo_FK = selected.TLDYE_NcrBatchNo_FK; nonD.DYENCCON_ConOrNon = true; nonD.DYENCCON_Code_FK = (int)row.Cells[1].Value; nonD.DYENCCON_ConNumber = selected.TLDYE_NcrNumber; nonD.DYENCCON_Qunatities = (decimal)row.Cells[4].Value; if (Add) { context.TLDYE_NonComplianceConsDetail.Add(nonD); } } foreach (DataGridViewRow row in dataGridView4.Rows) { if (row.Cells[1].Value == null) { continue; } Add = false; TLDYE_NonComplianceConsDetail nonD = new TLDYE_NonComplianceConsDetail(); // Cant add the same record twice if (row.Cells[0].Value != null) { var index = (int)row.Cells[0].Value; nonD = context.TLDYE_NonComplianceConsDetail.Find(index); } else { Add = true; } nonD.DYENCCON_BatchNo_FK = selected.TLDYE_NcrBatchNo_FK; nonD.DYENCCON_ConOrNon = false; nonD.DYENCCON_Code_FK = (int)row.Cells[1].Value; nonD.DYENCCON_ConNumber = selected.TLDYE_NcrNumber; if (row.Cells[2].Value != null) { nonD.DYENCCON_Qunatities = (decimal)row.Cells[2].Value; } else { nonD.DYENCCON_Qunatities = 0.00M; } if (Add) { context.TLDYE_NonComplianceConsDetail.Add(nonD); } } try { context.SaveChanges(); MessageBox.Show("Data saved successfully to database"); frmDyeViewReport vRep = new frmDyeViewReport(7, selected.TLDYE_NcrNumber); 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) { Button oBtn = sender as Button; if (oBtn != null && formloaded) { using (var context = new TTI2Entities()) { var Dept = context.TLADM_Departments.Where(x => x.Dep_ShortCode.Contains("DYE")).FirstOrDefault(); if (Dept != null) { TLDYE_NonCompliance nonC = new TLDYE_NonCompliance(); var LNU = context.TLADM_LastNumberUsed.Find(3); if (LNU != null) { nonC.TLDYE_ComplainceDate = dtpComplDate.Value; nonC.TLDYE_NcrBatchNo_FK = _DyeBatch; nonC.TLDYE_NcrNotes = rtbNotes.Text; nonC.TLDYE_NcrNumber = LNU.col5; nonC.TLDYE_Department_FK = Dept.Dep_Id; nonC.TLDYE_Operator_FK = _Operator; nonC.TLDYE_NCStage = _NCStage; var DBAllocated = context.TLDYE_DyeBatchAllocated.Where(x => x.TLDYEA_DyeBatch_FK == _DyeBatch).FirstOrDefault(); if (DBAllocated != null) { nonC.TLDYE_Machine_FK = DBAllocated.TLDYEA_MachCode_FK; } context.TLDYE_NonCompliance.Add(nonC); } foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString() == bool.FalseString) { continue; } TLDYE_NonComplianceDetail nonD = new TLDYE_NonComplianceDetail(); nonD.DYENCRD_BatchNo_Fk = _DyeBatch; nonD.DYENCRD_FR = true; nonD.DYENCRD_Code_FK = (int)row.Cells[0].Value; nonD.DYENCRD_ComNumber = LNU.col5; context.TLDYE_NonComplianceDetail.Add(nonD); } try { // Last Number used //------------------------------------------------ LNU.col5 += 1; context.SaveChanges(); MessageBox.Show("Data saved to database successfully"); frmDyeViewReport vRep = new frmDyeViewReport(7, nonC.TLDYE_NcrNumber); 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); } } } } }