private void btnColour_Click(object sender, EventArgs e) { Button oBtn = sender as Button; TLDYE_RecipeDefinition rd; if (oBtn != null && formloaded) { if (txtProductCode.Text.Length <= 0) { MessageBox.Show("Please enter a code prior to selecting this option"); return; } var ComboQual = (TLADM_GreigeQuality)cmboGreigeQuality.SelectedItem; if (ComboQual == null) { MessageBox.Show("Please select a Greige quality group prior to selecting this option"); return; } if (txtProgramLoad.Text.Length <= 0) { MessageBox.Show("Please enter a program load prior to selecting this option"); return; } if (txtProgramVolume.Text.Length <= 0) { MessageBox.Show("please enter a program volume prior to selecting this option"); return; } if (lNew) { using (var context = new TTI2Entities()) { rd = new TLDYE_RecipeDefinition(); rd.TLDYE_DefineCode = txtProductCode.Text; rd.TLDYE_DefineDescription = txtProductCode.Text + " " + ComboQual.GQ_Description; rd.TLDYE_ProgramLoad = Convert.ToInt32(txtProgramLoad.Text); rd.TLDYE_LiquidLoad = Convert.ToInt32(txtProgramVolume.Text); try { context.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.InnerException.ToString()); } } } else { rd = (TLDYE_RecipeDefinition)cmboProductCodes.SelectedItem; } using (frmDyeColourDefinition YOAssigned = new frmDyeColourDefinition(rd.TLDYE_DefinePk)) { DialogResult dr = YOAssigned.ShowDialog(this); if (dr == DialogResult.OK) { } } } }
private void btnSave_Click(object sender, EventArgs e) { Button oBtn = sender as Button; TLADM_Colours SelectedColour = null; bool lAdd = false; if (lNew) { lAdd = true; } if (rbStandard.Checked) { SelectedColour = (TLADM_Colours)cmboColours.SelectedItem; if (SelectedColour == null) { MessageBox.Show("Please select a colour from the drop down box provided"); return; } if (QueryParms.FabricQualities.Count == 0) { MessageBox.Show("Please add a Quality"); return; } } if (oBtn != null && formloaded) { var ErrorM = core.returnMessage(MandSelected, false, MandatoryFields); if (!String.IsNullOrEmpty(ErrorM)) { MessageBox.Show(ErrorM); return; } foreach (DataGridViewRow dr in dataGridView1.Rows) { if (rbStandard.Checked) { var tst = fieldEntered.Find(x => x.rownumber == dr.Index); if (tst.fieldComplete == null) { continue; } tst.fieldComplete[1] = true; var cnt = tst.fieldComplete.Where(x => x == false).Count(); if (cnt == MandatoryRows.Length) { continue; } cnt = tst.fieldComplete.Where(x => x == true).Count(); } } // 0 index of the main record // 1 ConsumablesDC // 2 Grams Per Litre // 3 Ratios // 4 Liquid Ratios using (var context = new TTI2Entities()) { TLDYE_RecipeDefinition rd = new TLDYE_RecipeDefinition(); if (!lAdd) { var selected = (TLDYE_RecipeDefinition)cmboProductCodes.SelectedItem; if (selected != null) { rd = context.TLDYE_RecipeDefinition.Find(selected.TLDYE_DefinePk); } } rd.TLDYE_DefineCode = txtProductCode.Text; if (rbStandard.Checked) { rd.TLDYE_DefineDescription = txtProductCode.Text + " " + SelectedColour.Col_Display; } else { rd.TLDYE_DefineDescription = txtProductCode.Text; } if (rbStandard.Checked) { rd.TLDYE_ProgramLoad = Convert.ToInt32(txtProgramLoad.Text); rd.TLDYE_LiquidLoad = Convert.ToInt32(txtProgramVolume.Text); rd.TLDYE_ColorChart_FK = SelectedColour.Col_Id; rd.TLDYE_StandardReceipe = true; } else { rd.TLDYE_LiquidLoad = 0; rd.TLDYE_ProgramLoad = 0; rd.TLDYE_ColorChart_FK = null; rd.TLDYE_StandardReceipe = false; } if (lAdd) { context.TLDYE_RecipeDefinition.Add(rd); } try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { MessageBox.Show("Property: " + validationError.PropertyName + " Error " + validationError.ErrorMessage); } return; } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // 0 index of the main record // 1 ConsumablesDC // 2 Grams Per Litre // 3 Ratios // 4 Liquid Ratios foreach (DataGridViewRow dr in dataGridView1.Rows) { if (dr.Cells[1].Value == null) { continue; } var tst = fieldEntered.Find(x => x.rownumber == dr.Index); if (tst.fieldComplete == null) { continue; } tst.fieldComplete[1] = true; var cnt = tst.fieldComplete.Where(x => x == false).Count(); if (cnt == MandatoryRows.Length) { continue; } lAdd = false; if (dr.Cells[0].Value == null) { lAdd = true; } cnt = tst.fieldComplete.Where(x => x == true).Count(); if (cnt == MandatoryRows.Length) { TLDYE_DefinitionDetails defdet = new TLDYE_DefinitionDetails(); if (!lAdd) { var index = Convert.ToInt32(dr.Cells[0].Value.ToString()); defdet = context.TLDYE_DefinitionDetails.Find(index); } defdet.TLDYED_Cosumables_FK = (int)dr.Cells[1].Value; if (dr.Cells[2].Value != null) { if ((bool)dr.Cells[2].Value == true) { defdet.TLDYED_LiqCalc = true; } else { defdet.TLDYED_LiqCalc = false; } } else { defdet.TLDYED_LiqCalc = false; } defdet.TLDYED_MELFC = (decimal)dr.Cells[3].Value; defdet.TLDYED_LiqRatio = (int)dr.Cells[4].Value; defdet.TLDYED_Receipe_FK = rd.TLDYE_DefinePk; if (lAdd) { context.TLDYE_DefinitionDetails.Add(defdet); } } } //--------------------------------------------------------- // //---------------------------------------------------------------- if (rbStandard.Checked) { //------------------------------------------------------------------------------------ // First we must ensure that any previous records that may exist are deleted and that we start with a clean slate //--------------------------------------------------------------------------- context.TLDYE_ReceipeGreigeQual.RemoveRange(context.TLDYE_ReceipeGreigeQual.Where(x => x.TLGQ_ReceipeDef_FK == rd.TLDYE_DefinePk)); //------------------------------------------------------ // new development //---------------------------------------------------------- foreach (var Qual in QueryParms.FabricQualities) { TLDYE_ReceipeGreigeQual repQual = new TLDYE_ReceipeGreigeQual(); repQual.TLGQ_GreigeQuality_FK = Qual.GQ_Pk; repQual.TLGQ_ReceipeDef_FK = rd.TLDYE_DefinePk; context.TLDYE_ReceipeGreigeQual.Add(repQual); } } try { context.SaveChanges(); MessageBox.Show("Data saved to database successfully"); } catch (Exception ex) { // MessageBox.Show(ex.Message); MessageBox.Show(ex.InnerException.ToString()); } finally { this.cmboGreigeQuality.Items.Clear(); dataGridView1.Rows.Clear(); SetUp(); } } } }