private void addMeasurements(Instrument objInstrument)
        {
            DataView objDataView = new DataView(CurrentDataTable);
            CalibrationMeasurement objMeasurement = new CalibrationMeasurement();
            StringBuilder strbldrInstrument = new StringBuilder();
            StringBuilder strbldrMeasurement = new StringBuilder();
            StringBuilder strbldrInstrumentColumnHeader = new StringBuilder();
            StringBuilder strbldrMeasurementColumnHeader = new StringBuilder();
            DateTime dtmTemp;

            //objRow[txtInstrumentIDHdr.Text].ToString().Replace(" ", "")

            objDataView.RowFilter = "[" + txtInstrumentIDHdr.Text + "] = '" + objInstrument.InstrumentID + "'";

            foreach (DataRowView objRow in objDataView)
            {
                for (int intCounter = 1; intCounter <= 3; intCounter++)
                {
                    strbldrInstrumentColumnHeader.Clear();
                    strbldrInstrumentColumnHeader.Append("Test #" + intCounter);
                    strbldrMeasurementColumnHeader.Clear();
                    strbldrMeasurementColumnHeader.Append("Result #" + intCounter);

                    if (string.IsNullOrEmpty(objRow[strbldrMeasurementColumnHeader.ToString()].ToString()) || string.IsNullOrEmpty(objRow[strbldrInstrumentColumnHeader.ToString()].ToString()))
                        break;
                    if (objInstrument.Measurements == null)
                        objInstrument.Measurements = new SortableSearchableBindingList<CalibrationMeasurement>();

                    strbldrInstrument.Clear();
                    strbldrInstrument.Append(objRow[strbldrInstrumentColumnHeader.ToString()].ToString());
                    strbldrMeasurement.Clear();
                    strbldrMeasurement.Append(objRow[strbldrMeasurementColumnHeader.ToString()].ToString());

                    try
                    {
                        objMeasurement = new CalibrationMeasurement
                        {
                            MeasuredBy = string.Empty,
                            Value = strbldrMeasurement.ToString(),
                            MeasurementDate = DateTime.TryParse(objRow[txtLCalDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                            MeasurementID = strbldrInstrumentColumnHeader.ToString()
                        };

                        objInstrument.Measurements.Add(objMeasurement);
                        db.SaveChanges();
                    }
                    catch (Exception objEx)
                    {
                        try //if there is a column mismatch then a nullreference exception is thown when the below log is written.  I place a try catch block here to handle it.
                        {
                            using (StreamWriter w = File.AppendText("log.txt"))//log error
                            {
                                SharedFunctions.Log(string.Format("Exception Error: Measurement for CheckFixture \r\n" +
                                "InstrumentID='{0}'\r\n" +
                                "MeasurementID='{1}'\r\n" +
                                "Value='{2}'\r\n" +
                                "Exception Details:{3}",
                                    objInstrument.InstrumentID, objMeasurement.MeasurementID, objMeasurement.Value, objEx), w);
                                w.Close();
                            }
                            objInstrument.Measurements.Remove(objMeasurement);//remove offending item from the list
                        }
                        catch (NullReferenceException objNullRefEx)
                        {
                            using (StreamWriter w = File.AppendText("log.txt"))//log error
                            {
                                SharedFunctions.Log(string.Format("Measurement Null Reference Exception:\r\n", objNullRefEx), w);
                            }
                        }
                        catch (InvalidOperationException objInvalidOpEx)
                        {
                            using (StreamWriter w = File.AppendText("log.txt"))//log error
                            {
                                SharedFunctions.Log(string.Format("Invalid Operation Exception: The app tried to remove a measurement from the collection that was never added\r\n", objInvalidOpEx), w);
                            }
                        }
                    }

                }
            }
        }
 private void ReturnSelectedItem()
 {
     if (dgvInstruments.SelectedRows.Count == 0)
         MessageBox.Show("No Instrument selected!", "Job", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
     else
     {
         SelectedInstrument = dgvInstruments.CurrentRow.DataBoundItem as Instrument;
         this.Dispose();
     }
 }
        private void btnUpload_Click(object sender, EventArgs e)
        {
            Instrument objInstrument = null;
            DateTime dtmTemp;
            //bool blnTemp;
            Location objSelectedLocation = db.Locations
                .Find(cmbLocation.SelectedItem.ToString());

            foreach (DataRow objRow in CurrentDataTable.Rows)
            {
                try
                {
                    if (chkIsRingGauges.Checked)
                        objInstrument = new Instrument
                        {
                            InstrumentID = (objRow[txtInstrumentIDHdr.Text].ToString() + "-" + objRow["PITCH       DIAMETER"].ToString() + "-" + objRow["Go          No Go"].ToString()).Replace(" ", ""),
                            Description = (objRow[txtDescriptionHdr.Text].ToString() + " CERTIFICATION: " + objRow["CERTIFICATION"].ToString()).Trim(),
                            Manufacturer = objRow[txtManufacturerHdr.Text].ToString().Trim(),
                            SerialNumber = objRow[txtSerialNoHdr.Text].ToString().Trim(),
                            LastCalibrationDate = SharedVariables.MINDATE,
                            NextCalibrationDate = SharedVariables.MINDATE,
                            Location = objSelectedLocation,
                            Procedure = objRow[txtProcedureHdr.Text].ToString().Trim(),
                            //OutOfService = bool.TryParse(objRow[txtOutOfServiceHdr.Text].ToString(), out blnTemp) ? blnTemp : false,
                            CalibratedBy = string.Empty
                        };
                    else
                        objInstrument = new Instrument
                        {
                            InstrumentID = string.IsNullOrEmpty(objRow[txtInstrumentIDHdr.Text].ToString()) ? string.Empty : objRow[txtInstrumentIDHdr.Text].ToString().Replace(" ", ""),
                            Description = objRow[txtDescriptionHdr.Text].ToString().Trim(),
                            Manufacturer = objRow[txtManufacturerHdr.Text].ToString().Trim(),
                            SerialNumber = objRow[txtSerialNoHdr.Text].ToString().Trim(),
                            LastCalibrationDate = DateTime.TryParse(objRow[txtLCalDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                            NextCalibrationDate = DateTime.TryParse(objRow[txtNCalDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                            Location = objSelectedLocation,
                            Procedure = objRow[txtProcedureHdr.Text].ToString().Trim(),
                            //OutOfServiceDate = DateTime.TryParse(objRow[txtOutOfServiceHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                            //OutOfService = bool.TryParse(objRow[txtOutOfServiceHdr.Text].ToString(), out blnTemp) ? blnTemp : false,
                            CalibratedBy = objRow[txtCalibratedByHdr.Text].ToString().Trim()
                        };
                    if (DateTime.TryParse(objRow[txtOutOfServiceHdr.Text].ToString(), out dtmTemp))//I moved this out of the constructor so that i could apply logic to determining if instrument is out of service.
                    {
                        objInstrument.OutOfServiceDate = dtmTemp;
                        objInstrument.OutOfService = true;
                    }
                    else
                    {
                        objInstrument.OutOfServiceDate = SharedVariables.MINDATE;
                        objInstrument.OutOfService = false;
                    }

                    if (objInstrument.isValid)
                    {
                        db.Instruments.Add(objInstrument);
                        db.SaveChanges();
                        addMeasurements(objInstrument);
                    }
                }
                catch (Exception objEx)
                {
                    try //if there is a column mismatch then a nullreference exception is thown when the below log is written.  I place a try catch block here to handle it.
                    {
                        using (StreamWriter w = File.AppendText("log.txt"))//log error
                        {
                            SharedFunctions.Log(string.Format("Exception Error: Instrument having\r\n" +
                            "InstrumentID='{0}'\r\n" +
                            "Description='{1}'\r\n" +
                            "Manufacturer='{2}'\r\n" +
                            "SerialNumber='{3}'\r\n" +
                            "LastCalibrationDate='{4}'\r\n" +
                            "NextCalibrationDate='{5}'\r\n" +
                            "Location='{6}'\r\n" +
                            "Procedure='{7}'\r\n" +
                            "OutOfService='{8}'\r\n" +
                            "CalibratedBy='{9}'\r\n" +
                            "Exception Details:{10}",
                                objInstrument.InstrumentID, objInstrument.Description, objInstrument.Manufacturer, objInstrument.SerialNumber, objInstrument.LastCalibrationDate.ToShortDateString(),
                                objInstrument.NextCalibrationDate.ToShortDateString(), objInstrument.Location.ToString(), objInstrument.Procedure, objInstrument.OutOfService, objInstrument.CalibratedBy, objEx), w);
                            w.Close();
                        }
                        db.Instruments.Remove(objInstrument);//remove offending item from the list
                    }
                    catch (NullReferenceException objNullRefEx)
                    {
                        using (StreamWriter w = File.AppendText("log.txt"))//log error
                        {
                            SharedFunctions.Log(string.Format("Null Reference Exception: Check your columns for mismatch\r\n", objNullRefEx), w);
                        }
                    }
                    catch (InvalidOperationException objInvalidOpEx)
                    {
                        using (StreamWriter w = File.AppendText("log.txt"))//log error
                        {
                            SharedFunctions.Log(string.Format("Invalid Operation Exception: The app tried to remove an item from the collection that was never added\r\n", objInvalidOpEx), w);
                        }
                    }
                }
            }
            MessageBox.Show("Completed");
        }