/// <summary> /// Handles the Click event of the btnUpdate control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnUpdate_Click(object sender, EventArgs e) { if (updateRows.Count > 0) // redundant check - should not be enabled if no update has occurred { // validate Results. if (validateResults()) { List <LocaltblIntermeditateICP> interICPList = new List <LocaltblIntermeditateICP>(); ICPClientDataContext icpClientCtx = new ICPClientDataContext(); int row = 0; while (tlpDetails.GetControlFromPosition(0, row) != null) { LocaltblIntermeditateICP newInterICP = null; Label lbI = tlpDetails.GetControlFromPosition(0, row) as Label; Label lbE = tlpDetails.GetControlFromPosition(1, row) as Label; Label lbW = tlpDetails.GetControlFromPosition(2, row) as Label; TextBox tb = tlpDetails.GetControlFromPosition(3, row) as TextBox; CheckBox cbF = tlpDetails.GetControlFromPosition(4, row) as CheckBox; CheckBox cbC = tlpDetails.GetControlFromPosition(5, row) as CheckBox; CheckBox cbD = tlpDetails.GetControlFromPosition(6, row) as CheckBox; if (updateRows.Contains(row)) { var existingInterICP = from exist in icpClientCtx.LocaltblIntermeditateICPs where exist.BarCode == updateICP.barCode && exist.Element == lbE.Text && exist.IndexLine == (Convert.ToInt16(lbI.Text)) select exist; if (existingInterICP.Count() > 0) { // update existing record newInterICP = existingInterICP.FirstOrDefault(); newInterICP.AverageResult = Convert.ToDecimal(tb.Text); newInterICP.CheckFlag = (cbC.Checked) ? "F" : " "; newInterICP.FailFlags = (cbF.Checked) ? "C" : " "; newInterICP.DeleteFlag = cbD.Checked; } else { // insert record into database newInterICP = new LocaltblIntermeditateICP(); newInterICP.Anadate = updateICP.anaDate; newInterICP.AverageResult = Convert.ToDecimal(tb.Text); newInterICP.BarCode = updateICP.barCode; newInterICP.CheckFlag = (cbC.Checked) ? "F" : " "; newInterICP.Element = lbE.Text; newInterICP.Wavelength = lbW.Text; newInterICP.FailFlags = (cbF.Checked) ? "C" : " "; newInterICP.Code = updateICP.code; newInterICP.Duplicate = updateICP.duplicate; newInterICP.IndexLine = Convert.ToInt16(lbI.Text); newInterICP.DeleteFlag = cbD.Checked; icpClientCtx.LocaltblIntermeditateICPs.InsertOnSubmit(newInterICP); } try { icpClientCtx.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Database Update Failed: " + newInterICP.Element + "\n\n\t" + ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { newInterICP = new LocaltblIntermeditateICP(); newInterICP.Anadate = updateICP.anaDate; newInterICP.AverageResult = Convert.ToDecimal(tb.Text); newInterICP.BarCode = updateICP.barCode; newInterICP.CheckFlag = (cbC.Checked) ? "F" : " "; newInterICP.Element = lbE.Text; newInterICP.Wavelength = lbW.Text; newInterICP.FailFlags = (cbF.Checked) ? "C" : " "; newInterICP.Code = updateICP.code; newInterICP.Duplicate = updateICP.duplicate; newInterICP.IndexLine = Convert.ToInt16(lbI.Text); newInterICP.DeleteFlag = cbD.Checked; } interICPList.Add(newInterICP); row++; } this.DialogResult = DialogResult.OK; this.Tag = interICPList; } } }
/// <summary> /// Populates the detail table from the updateICP attribute. /// </summary> private void populateDetailTable() { ICPClientDataContext icpClientCtx = new ICPClientDataContext(); var intermediateICPList = from interICP in icpClientCtx.LocaltblIntermeditateICPs where interICP.BarCode == updateICP.barCode select interICP; TevaICPDataContext tevaIcpCtx = new TevaICPDataContext(); var elementList = from elem in tevaIcpCtx.ElementLines where elem.Sample.Name.Equals(updateICP.barCode) && !elem.ElementSymbol.Equals("Y") select elem; int col = 0; int row = 0; foreach (var element in elementList) { decimal aveResult = Convert.ToDecimal(element.AverageResult.Value); bool failFlag = element.FailFlags.Trim().Length > 0; bool checkFlag = element.CheckFlag.HasValue && element.CheckFlag.Value != ' '; bool deleteFlag = false; // getting compiler errors here with existingElem.checkflag which is a string, // but is being treated here as object. I re-created this data base dbml to add log table // this may have caused a newer version of compiler to balk at the use of HasValue and Value // for a string //if (intermediateICPList.Count() > 0) //{ // foreach (var existingElem in intermediateICPList) // { // if (existingElem.Element == element.ElementSymbol && // existingElem.IndexLine.Value == element.LineIndex) // { // aveResult = existingElem.AverageResult.Value; // failFlag = existingElem.FailFlags.Trim().Length > 0; // checkFlag = existingElem.CheckFlag.HasValue && // existingElem.CheckFlag.Value != " "; // deleteFlag = (existingElem.DeleteFlag.HasValue) ? // existingElem.DeleteFlag.Value : false; // break; // } // } //} // check if a manual override exists // changed the logic here for strings if (intermediateICPList.Count() > 0) { foreach (var existingElem in intermediateICPList) { if (existingElem.Element == element.ElementSymbol && existingElem.IndexLine.Value == element.LineIndex) { aveResult = existingElem.AverageResult.Value; failFlag = existingElem.FailFlags.Trim().Length > 0; checkFlag = (existingElem.CheckFlag.Length > 0) && // use length rather than hasValue existingElem.CheckFlag != " "; // remove the .Value deleteFlag = (existingElem.DeleteFlag.HasValue)? existingElem.DeleteFlag.Value : false; break; } } } Label lblIndex = new Label(); lblIndex.TextAlign = ContentAlignment.MiddleCenter; lblIndex.Anchor = AnchorStyles.None; lblIndex.Text = element.LineIndex.ToString(); tlpDetails.Controls.Add(lblIndex, col, row); col++; Label lblElement = new Label(); lblElement.TextAlign = ContentAlignment.MiddleCenter; lblElement.Anchor = AnchorStyles.None; lblElement.Text = element.ElementSymbol; tlpDetails.Controls.Add(lblElement, col, row); col++; Label lblWavelength = new Label(); lblWavelength.TextAlign = ContentAlignment.MiddleCenter; lblWavelength.Anchor = AnchorStyles.None; lblWavelength.Text = element.Wavelength; tlpDetails.Controls.Add(lblWavelength, col, row); col++; TextBox txtAveResult = new TextBox(); txtAveResult.TextAlign = HorizontalAlignment.Center; txtAveResult.Anchor = AnchorStyles.None; txtAveResult.Text = aveResult.ToString(); txtAveResult.TextChanged += new EventHandler(txtAveResult_TextChanged); tlpDetails.Controls.Add(txtAveResult, col, row); col++; CheckBox cbFailFlag = new CheckBox(); cbFailFlag.Anchor = AnchorStyles.None; cbFailFlag.Text = string.Empty; cbFailFlag.CheckedChanged += new EventHandler(cbFlag_CheckedChanged); cbFailFlag.Checked = failFlag; tlpDetails.Controls.Add(cbFailFlag, col, row); col++; CheckBox cbCheckFlag = new CheckBox(); cbCheckFlag.Anchor = AnchorStyles.None; cbCheckFlag.Text = string.Empty; cbCheckFlag.CheckedChanged += new EventHandler(cbFlag_CheckedChanged); cbCheckFlag.Checked = checkFlag; tlpDetails.Controls.Add(cbCheckFlag, col, row); col++; CheckBox cbDeleteFlag = new CheckBox(); cbDeleteFlag.Anchor = AnchorStyles.None; cbDeleteFlag.Text = string.Empty; cbDeleteFlag.CheckedChanged += new EventHandler(cbFlag_CheckedChanged); cbDeleteFlag.Checked = deleteFlag; tlpDetails.Controls.Add(cbDeleteFlag, col, row); col = 0; row++; } }
/// <summary> /// Validates the limit insert data. /// </summary> /// <returns></returns> private bool validateLimitInsert(List <decimal> insertValues) { decimal reportLimit = 0.00M; decimal mdl = 0.00M; decimal diff = 0.00M; bool retVal = true; StringBuilder errMsg = new StringBuilder("The new record can not be inserted due to the following errors: \n\n"); if (txtElement.Text.Length == 0 || txtElement.Text.Length > 2) { retVal = false; errMsg.AppendLine("\tInvalid Element Value: Must be less than or equal to 2 characters."); } else { ICPClientDataContext ctx = new ICPClientDataContext(); var existingLimits = from lim in ctx.tblLimits where lim.Element == txtElement.Text select lim; if (existingLimits != null && existingLimits.Count() > 0) { retVal = false; errMsg.AppendLine("\tInvalid Element Value: Element already exists in database."); } ctx.Dispose(); } if (txtReportLimit.Text.Length == 0 || !Decimal.TryParse(txtReportLimit.Text, out reportLimit)) { retVal = false; errMsg.AppendLine("\tInvalid Reporting Limit Value: Can not be blank and must be a decimal number."); } if (txtMDL.Text.Length == 0 || !Decimal.TryParse(txtMDL.Text, out mdl)) { retVal = false; errMsg.AppendLine("\tInvalid MDL Value: Can not be blank and must be a decimal number."); } if (txtDvsTDiff.Text.Length == 0 || !Decimal.TryParse(txtDvsTDiff.Text, out diff)) { retVal = false; errMsg.AppendLine("\tInvalid Difference Value: Can not be blank and must be a decimal number."); } if (!retVal) { MessageBox.Show(errMsg.ToString(), "Insert Errors", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } else { insertValues.Add(reportLimit); insertValues.Add(mdl); insertValues.Add(diff); } return(retVal); }