private void addMeasurements(Fixture objCheckFixture)
        {
            DataView objDataView = new DataView(CurrentDataTable);
            Measurement objMeasurement = new Measurement();
            StringBuilder strbldrInstrument = new StringBuilder();
            StringBuilder strbldrMeasurement = new StringBuilder();
            StringBuilder strbldrInstrumentColumnHeader = new StringBuilder();
            StringBuilder strbldrMeasurementColumnHeader = new StringBuilder();
            DateTime dtmTemp;
            var objInstrumentList = db.Instruments.ToList();
            Instrument objDefaultInstrument = objInstrumentList.Single(i => i.InstrumentID == "NotFound");

            objDataView.RowFilter = "[" + txtFixtureIDHdr.Text + "] = '" + objCheckFixture.CheckFixtureID + "'";

            foreach (DataRowView objRow in objDataView)
            {
                for (int intCounter = 1; intCounter <= nudMeasurementCount.Value; intCounter++)
                {
                    strbldrInstrumentColumnHeader.Clear();
                    strbldrInstrumentColumnHeader.Append("inst" + intCounter);
                    strbldrMeasurementColumnHeader.Clear();
                    strbldrMeasurementColumnHeader.Append("m" + intCounter);

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

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

                    try
                    {
                        objMeasurement = new Measurement
                        {
                            InstrumentUsed = objInstrumentList
                                .Where(i => i.InstrumentID == strbldrInstrument.ToString().Trim())
                                .DefaultIfEmpty(objDefaultInstrument)
                                .SingleOrDefault(),
                            MeasuredBy = string.Empty,
                            Value = strbldrMeasurement.ToString(),
                            MeasurementDate = DateTime.TryParse(objRow[txtLCalibrationDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                            MeasurementID = strbldrMeasurementColumnHeader.ToString()
                        };

                        objCheckFixture.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" +
                                "CheckFixtureID='{0}'\r\n" +
                                "MeasurementID='{1}'\r\n" +
                                "Value='{2}'\r\n" +
                                "Exception Details:{3}",
                                    objCheckFixture.CheckFixtureID, objMeasurement.MeasurementID, objMeasurement.Value, objEx), w);
                                w.Close();
                            }
                            objCheckFixture.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 btnUpload_Click(object sender, EventArgs e)
        {
            Fixture objCheckFixture = null;
            DateTime dtmTemp;
            List<Fixture> objCheckFixtureList = new List<Fixture>();

            foreach (DataRow objRow in CurrentDataTable.Rows)
            {
                string strLocation = string.IsNullOrEmpty(objRow[txtLocation2Hdr.Text].ToString()) ?
                        objRow[txtLocation1Hdr.Text].ToString() : objRow[txtLocation1Hdr.Text].ToString() + " or " + objRow[txtLocation2Hdr.Text].ToString();

                objCheckFixture = new Fixture
                {
                    CheckFixtureID = objRow[txtFixtureIDHdr.Text].ToString().Trim(),
                    Description = string.Empty,
                    Location = strLocation,
                    LastCalibrationDate = DateTime.TryParse(objRow[txtLCalibrationDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                    NextCalibrationDate = DateTime.TryParse(objRow[txtNCalibrationDateHdr.Text].ToString(), out dtmTemp) ? dtmTemp : SharedVariables.MINDATE,
                    MeasuredBy = string.Empty
                };

                objCheckFixtureList.Add(objCheckFixture);
            }

            /*Here I take my group of objects that I collected above and group them by fixture(so I can make my fixture list with a pk CheckFixtureID), I group them by CheckFixtureID
             * take the other properties that I need and put them into an anonymous object list. Later I iterate on that anonymous object list and populate another list of CheckFixtures
             * with the values.The location property needed to result in a string of all the locations from the above collection delimited by the word "or". The last calibration date had to
             * be the most recent calibration date. All this was accomplished with the linq query below.
             */
            var qryFlattenedCollection = objCheckFixtureList
                .GroupBy(c => c.CheckFixtureID)
                .Select(eg => new { eg.Key, Location = string.Join(" or ", eg.Select(x => x.Location).Where(x => x.Trim().Length > 0)), LastCalibrationDate = eg.Max(x => x.LastCalibrationDate),
                    NextCalibrationDate = eg.Max(x => x.NextCalibrationDate)})
                .OrderByDescending(x => x.Key)
                .ToList();

            //.OrderByDescending(s => s.Location.Length).First()
            //.Max(x => x.Location.Length)
            foreach (var objTempCheckFixture in qryFlattenedCollection)
            {
                try
                {
                    objCheckFixture = new Fixture
                    {
                        CheckFixtureID = objTempCheckFixture.Key,
                        Description = string.Empty,
                        Location = objTempCheckFixture.Location,
                        LastCalibrationDate = objTempCheckFixture.LastCalibrationDate,
                        NextCalibrationDate = objTempCheckFixture.NextCalibrationDate,
                        MeasuredBy = string.Empty
                    };

                    objCheckFixtureList.Add(objCheckFixture);

                    if (objCheckFixture.isValid)
                    {
                        db.CheckFixtures.Add(objCheckFixture);
                        db.SaveChanges();
                        addMeasurements(objCheckFixture);
                    }
                }
                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: CheckFixture having\r\n" +
                            "CheckFixtureID='{0}'\r\n" +
                            "Description='{1}'\r\n" +
                            "Location='{2}'\r\n" +
                            "LastCalibrationDate='{3}'\r\n" +
                            "NextCalibrationDate='{4}'\r\n" +
                            "MeasuredBy='{5}'\r\n" +
                            "Exception Details:{6}",
                                objCheckFixture.CheckFixtureID, objCheckFixture.Description, objCheckFixture.Location, objCheckFixture.LastCalibrationDate.ToShortDateString(),
                                objCheckFixture.NextCalibrationDate.ToShortDateString(), objCheckFixture.MeasuredBy, objEx), w);
                            w.Close();
                        }
                        db.CheckFixtures.Remove(objCheckFixture);//remove offending item from the list
                        //db.SaveChanges();
                    }
                    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");
        }