public void FormatData(string packet, DatabaseDataSet db) { string[] newContent = packet.Split(','); string reader_mac; string readings; if (newContent.LastOrDefault().Length > 2) // to check that sensors return { reader_mac = newContent[newContent.Length - 2]; // to get the reader mac (R1) readings = string.Join(",", newContent, 1, newContent.Length - 3); string sensors = newContent[newContent.Length - 1]; readings += "," + sensors; } else if (newContent.LastOrDefault().Length < 2) { reader_mac = newContent[newContent.Length - 2]; readings = string.Join(",", newContent, 1, newContent.Length - 3); } else { reader_mac = newContent[newContent.Length - 1]; readings = string.Join(",", newContent, 1, newContent.Length - 2); } string readingbefore = readings; List <string> RSSI_string = readingbefore.Split(',').ToList(); // Sensors go here..... // SensorsFromBeacon = RSSI_string[RSSI_string.Count-1]; // RSSI_string.RemoveAt(RSSI_string.Count - 1); EntityCommand db = new EntityCommand(); if (RSSI_string.Count() != 0) { string Beacon_mac = newContent[0]; var beacon_found = db.pcptgs006.FirstOrDefault(b => b.pcptgs006002 == Beacon_mac); if (beacon_found != null) { using (pcptgs014 pcptgs014 = new pcptgs014()) { TimeSpan TodayTime = DateTime.Now.TimeOfDay; DateTime today = DateTime.Now; pcptgs014.pcptgs014001 = today; pcptgs014.pcptgs014002 = TodayTime; pcptgs014.pcptgs014003 = reader_mac; pcptgs014.pcptgs014004 = Beacon_mac; pcptgs014.pcptgs014005 = readings; db.pcptgs014.Add(pcptgs014); try { db.SaveChanges(); } catch (Exception ex) { } } } } }