public void Sync() { // Disable the buttons. IsExitEnabled = false; IsTryEnabled = false; IsStartOverEnabled = false; OnPropertyChanged("IsExitEnabled"); OnPropertyChanged("IsTryEnabled"); OnPropertyChanged("IsStartOverEnabled"); // Reset error count. ValidationErrorCount = 0; SaveErrorCount = 0; StatusText = string.Format("{0} - Starting.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); var service = new QuickBooksService(); StatusText += string.Format("{0} - Connecting to QuickBooks.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); // QBXML request processor must always be closed after use. var req = new RequestProcessor2(); try { req.OpenConnection2("", "BRIZBEE Integration Utility", QBXMLRPConnectionType.localQBD); var ticket = req.BeginSession("", QBFileMode.qbFileOpenDoNotCare); StatusText += string.Format("{0} - Syncing.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); // Get projects from the server. var projects = GetProjects(); foreach (var project in projects) { if (string.IsNullOrEmpty(project)) { continue; } var split = project.Split(':'); var name = project; // Default to original name var parentName = ""; // Customer and Job if (split.Length > 1) { name = split[1]; parentName = split[0]; // Prepare a new QBXML document to find the parent and its details. var findQBXML = service.MakeQBXMLDocument(); var findDocument = findQBXML.Item1; var findElement = findQBXML.Item2; // Build the request to find the parent. service.BuildCustomerQueryRq(findDocument, findElement, parentName); // Make the request. Logger.Debug(findDocument.OuterXml); var findResponse = req.ProcessRequest(ticket, findDocument.OuterXml); Logger.Debug(findResponse); // Then walk the response. var findWalkResponse = service.WalkCustomerQueryRs(findResponse); if (findWalkResponse.Item1) { var found = findWalkResponse.Item3; var parent = found.FirstOrDefault(); // Prepare a new QBXML document to create the job with customer details. var jobQBXML = service.MakeQBXMLDocument(); var jobDocument = jobQBXML.Item1; var jobElement = jobQBXML.Item2; var newJob = parent; newJob.Name = name; newJob.ListId = ""; // Build the request to create the job. service.BuildCustomerAddRqForJob(jobDocument, jobElement, newJob, parentName); // Make the request. Logger.Debug(jobDocument.OuterXml); var jobResponse = req.ProcessRequest(ticket, jobDocument.OuterXml); Logger.Debug(jobResponse); } else { // Prepare a new QBXML document to create the parent. var custQBXML = service.MakeQBXMLDocument(); var custDocument = custQBXML.Item1; var custElement = custQBXML.Item2; // Build the request to create the parent. service.BuildCustomerAddRqForCustomer(custDocument, custElement, parentName); // Make the request. Logger.Debug(custDocument.OuterXml); var custResponse = req.ProcessRequest(ticket, custDocument.OuterXml); Logger.Debug(custResponse); // Prepare a new QBXML document to create the job. var jobQBXML = service.MakeQBXMLDocument(); var jobDocument = jobQBXML.Item1; var jobElement = jobQBXML.Item2; // Build the request to create the job. service.BuildCustomerAddRqForJob(jobDocument, jobElement, new QuickBooksCustomer() { Name = name }, parentName); // Make the request. Logger.Debug(jobDocument.OuterXml); var jobResponse = req.ProcessRequest(ticket, jobDocument.OuterXml); Logger.Debug(jobResponse); } } // Customer Only else { // Prepare a new QBXML document. var custQBXML = service.MakeQBXMLDocument(); var custDocument = custQBXML.Item1; var custElement = custQBXML.Item2; // Build the request to create the customer. service.BuildCustomerAddRqForCustomer(custDocument, custElement, name); // Make the request. var custResponse = req.ProcessRequest(ticket, custDocument.OuterXml); } } StatusText += string.Format("{0} - Finishing Up.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); StatusText += string.Format("{0} - Synced Successfully.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); // Close the QuickBooks connection. req.EndSession(ticket); req.CloseConnection(); req = null; } catch (COMException cex) { Logger.Error(cex.ToString()); if ((uint)cex.ErrorCode == 0x80040408) { StatusText += string.Format("{0} - Sync failed. QuickBooks Desktop is not open.\r\n", DateTime.Now.ToString()); OnPropertyChanged("StatusText"); } else { StatusText += string.Format("{0} - Sync failed. {1}\r\n", DateTime.Now.ToString(), cex.Message); OnPropertyChanged("StatusText"); } } catch (Exception ex) { Logger.Error(ex.ToString()); StatusText += string.Format("{0} - Sync failed. {1}\r\n", DateTime.Now.ToString(), ex.Message); OnPropertyChanged("StatusText"); } finally { // Enable the buttons. IsExitEnabled = true; IsTryEnabled = true; IsStartOverEnabled = true; OnPropertyChanged("IsExitEnabled"); OnPropertyChanged("IsTryEnabled"); OnPropertyChanged("IsStartOverEnabled"); // Try to close the QuickBooks connection if it is still open. if (req != null) { req.CloseConnection(); req = null; } } }