private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { var args = e.Argument as object[]; var monthSelected = (DateTime)args[0]; var shipmentType = (string)args[1]; var invController = new InvoiceController(_dbConnection, _dbEdidata1); var tranController = new TransactionController(_dbConnection, _dbEdidata1); var cultureInfo = new System.Globalization.CultureInfo("en-US"); var lastDays = new DateTime(monthSelected.Year, monthSelected.Month, DateTime.DaysInMonth(monthSelected.Year, monthSelected.Month)); var fromDate = monthSelected.ToString("yyyyMMdd", cultureInfo); var toDate = lastDays.ToString("yyyyMMdd", cultureInfo); List <Invoice> invoices = new List <Invoice>(); List <Shipment> shipments = new List <Shipment>(); // Load Commercial invoice. if (shipmentType.ToUpper() == "EXPORT") { invoices = invController.ExportShipmentResult(fromDate, toDate); shipments = tranController.ExportShipmentResult(fromDate, toDate); } else { invoices = invController.ImportShipmentResult(fromDate, toDate); shipments = tranController.ImportShipmentResult(fromDate, toDate); } for (int i = 0; i < invoices.Count; i++) { var shipment = shipments.Where(q => q.DecNO.TrimEnd() == invoices.ElementAt(i).DecNo.TrimEnd()); if (shipment == null || shipment.Count() == 0) { invoices.ElementAt(i).Status = "I"; } else { invoices.ElementAt(i).Status = shipment.ElementAt(0).Status; invoices.ElementAt(i).RawCode = shipment.ElementAt(0).MaterialType; invoices.ElementAt(i).Branch = shipment.ElementAt(0).BranchSeq; if (shipment.ElementAt(0).Status == "A") { invoices.ElementAt(i).Location = string.Format("{0}/{1}.pdf", shipmentType.ToUpper() == "EXPORT" ? _ftpExpRoot : _ftpImpRoot, invoices.ElementAt(i).DecNo.TrimEnd()); } } } invoices.ForEach(q => { if (q.Status == "I") { Shipment chkShipment; try { if (shipmentType.ToUpper() == "IMPORT") { chkShipment = tranController.ImportShipmentVerifyResult(q.DecNo.TrimEnd()); } else { chkShipment = tranController.ExportShipmentVerifyResult(q.DecNo.TrimEnd()); } if (chkShipment != null) { q.RawCode = chkShipment.MaterialType; q.Branch = chkShipment.BranchSeq; q.Status = chkShipment.Status; q.Location = string.Format("{0}/{1}.pdf", shipmentType.ToUpper() == "EXPORT" ? _ftpExpRoot : _ftpImpRoot, q.DecNo.TrimEnd()); } } catch { } } }); e.Result = invoices; } catch { } }