public static void RefreshDeliveryReport() { using (var dataBinding = new MessageDataAccessor()) using (var service = new MyMobileAPIService.APISoapClient()) { DataSet dataSet = null; DataTable settingsTable = null; foreach (var account in dataBinding.GetAccounts()) { bool recordsProcessed; do { recordsProcessed = false; SentServiceCallEntity data = new SentServiceCallEntity() { Settings = dataBinding.GetSentSettings(account.AccountId) }; dataSet = new DataSet("sent"); settingsTable = new DataTable("settings"); settingsTable.Columns.Add("id"); settingsTable.Columns.Add("max_recs"); settingsTable.Columns.Add("cols_returned"); settingsTable.Columns.Add("date_format"); DataRow mainRow = settingsTable.NewRow(); mainRow["id"] = data.Settings.LatestId; mainRow["max_recs"] = data.Settings.RecordCount; mainRow["cols_returned"] = data.Settings.ReturnColumns; mainRow["date_format"] = data.Settings.DateFormat; settingsTable.Rows.Add(mainRow); dataSet.Tables.Add(settingsTable); DataSet results = service.Sent_DS_DS(account.Username, account.Password, dataSet); SaveXmlStringToFile(results.GetXml(),"DELIVERIES"); if (results != null && results.Tables.Contains("data")) { results.Tables["data"].AsEnumerable().ForAll(r => { //var r = results.Tables["data"].Rows[0]; Guid tempGuid; MessageEntity message; try { if (Guid.TryParse(TypeSafety.GetValue<string>(r["customerid"]), out tempGuid)) { message = dataBinding.GetMessageByRowGuid(tempGuid); // we only update the values of an existing message if (message != null) { message.NumberTo = TypeSafety.GetValue<string>(r["numto"]); message.NumberTo = (message.NumberTo.Length > 15) ? message.NumberTo.Substring(0, 15) : message.NumberTo; message.Text = TypeSafety.GetValue<string>(r["data"]); message.EventId = TypeSafety.GetValue<int>(r["eventid"]); message.Status = TypeSafety.GetValue<string>(r["status"]); } else { message = new MessageEntity() { // we create a new message as it does not exist NumberTo = TypeSafety.GetValue<string>(r["numto"]), Sender = string.Empty, Text = TypeSafety.GetValue<string>(r["data"]), DateCreated = DateTime.Now, OriginId = 5, // Legacy System UniqueId = TypeSafety.GetValue<int>(r["customerid"], 0), EventId = TypeSafety.GetValue<int>(r["eventid"]), RowGuid = Guid.NewGuid(), Status = "UNKOWN " + TypeSafety.GetValue<string>(r["status"]) }; } message = dataBinding.SetMessageForced(message); } else { message = new MessageEntity() { // we create a new message as it does not exist NumberTo = TypeSafety.GetValue<string>(r["numto"]), Sender = string.Empty, Text = TypeSafety.GetValue<string>(r["data"]), DateCreated = DateTime.Now, OriginId = 5, // Legacy System UniqueId = TypeSafety.GetValue<int>(r["customerid"], 0), EventId = TypeSafety.GetValue<int>(r["eventid"]), RowGuid = Guid.NewGuid(), Status = "UNKOWN " + TypeSafety.GetValue<string>(r["status"]) }; message = dataBinding.SetMessageForced(message); } if (message != null) { DeliveryReportEntity report = new DeliveryReportEntity(); report.ChangeId = TypeSafety.GetValue<long>(r["changeid"]); report.MessageId = message.MessageId; report.SentId = TypeSafety.GetValue<long>(r["sentid"].ToString()); report.Status = TypeSafety.GetValue<string>(r["status"]); report.StatusDate = TypeSafety.GetValue<DateTime>(r["statusdate"], DateTime.Now); dataBinding.InsertDeliveryReport(report); } } catch (Exception ex) { System.Threading.Thread.Sleep(1); } } ); recordsProcessed = results.Tables["data"].Rows.Count > 0; if (recordsProcessed) { long latestID = results.Tables["data"].AsEnumerable().Max(s => TypeSafety.GetValue<long>(s["changeid"])); dataBinding.UpdateSentSettingsLatestID(latestID, account.AccountId); } } } while (recordsProcessed); } } }