Reputation: 1
So Quickbooks does not allow a windows service to connect to its system through its QBFC13.0. It also does not allow any program to run automatically, everything needs to run in the context of a USER. I tried everything to get around this. Where I am at now: I have a windows application (.exe) that is manually started by a user. It loops infinitely, but at 4 a.m. it is supposed to do some data gathering from QuickBooks. The work it performs does succeed (sometimes).
The issue appears randomly when it attempts to do work during its 4 a.m. time slot. The task manager shows it's still running but the log files don't show any more entries. When it's in its loop it's supposed to log every minute saying it's active. I can't fathom how it stops writing to log (or do anything really) yet it's still running in task manager. In addition, when it stops it doesn't write anything to the log, no errors caught.
I have not done any code cleanup so I am sorry if its not organized well and is not proper.
static void Main(string[] args)
{
WriteToFile("Program Startup " + DateTime.Now);
try
{
var DebugLevel = System.Configuration.ConfigurationManager.AppSettings["Debug"];
while (1 > 0)
{
Thread.Sleep(1000 * 60);
WriteToFile("Timer Ticked " + DateTime.Now);
if ((DateTime.Now.Hour == 4 && DateTime.Now.Minute == 0 && attempt == -1) || DebugLevel == "DEBUG" || (attempt <= 2 && attempt >= 0))
{
GC.Collect();
GC.WaitForPendingFinalizers();
try
{
attempt++;//iterate the attempt
WriteToFile("QuickBooks data gather executable started " + DateTime.Now + " Attempt Number: " + Convert.ToString(attempt + 1));
SetupQBSession();
QBCustomer customerQuery = new QBCustomer();
List<ICustomerRet> customers = customerQuery.DoCustomerQuery(sessionManager, connectionOpen, sessionBegun);
WriteToFile("Initial Connection to Quickbooks succesfull. " + DateTime.Now);
foreach (ICustomerRet r in customers)
{
//WriteToFile(r.Name.GetValue());
//Make database Call to log QB customer data.
using (CSTClassesDataContext cst = new CSTClassesDataContext(ConfigurationManager.ConnectionStrings["CST"].ConnectionString))
{
//save the customer data
var DBCustomerSaveResult = cst.QB_Customer_Save(r.Name.GetValue(), (bool)r.IsActive.GetValue());
WriteToFile("QuickBooks Customer Data Saved for " + r.Name.GetValue() + " " + DateTime.Now);
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
SqlDataReader dataReader;
connetionString = cst.Connection.ConnectionString;
sql = "select * from QB_customer where QB_customer.customer_name = '" + r.Name.GetValue() + "'";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
dataReader = command.ExecuteReader();
if (dataReader.Read() && dataReader.HasRows)
{
QBInvoice invoicesQuery = new QBInvoice();
List<IInvoiceRet> invoices = invoicesQuery.DoInvoiceQuery(dataReader.GetValue(1).ToString(), sessionManager, connectionOpen, sessionBegun);
foreach (IInvoiceRet invoice in invoices)
{
if (invoice.Memo.GetValue().ToString() != null)
{
int dbQBcustomerID = Convert.ToInt32(dataReader.GetValue(0));
var DBInvoiceSaveResult = cst.QB_invoice_save(dbQBcustomerID, invoice.RefNumber.GetValue().ToString(), invoice.Memo.GetValue().ToString(), (decimal)invoice.BalanceRemaining.GetValue(), invoice.DueDate.GetValue(), invoice.IsPaid.GetValue());
WriteToFile("Invoices updated for QB_customer_id: " + dbQBcustomerID + ", Invoice Number " + invoice.RefNumber.GetValue().ToString() + ", IsPaid " + invoice.IsPaid.GetValue() + " " + DateTime.Now);
}
}
//Console.WriteLine(dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + " - " + dataReader.GetValue(2));
}
dataReader.Close();
command.Dispose();
connection.Close();
}
catch (Exception ex)
{
WriteToFile(ex.ToString());
}
}
}
CloseQBSession();
attempt = -1;
}
catch (Exception exeption)
{
if (attempt <= 2 && attempt >= 0)
{
//attempt++;
}
else
{
attempt = -1;
}
CloseQBSession();
WriteToFile(exeption.ToString());
}
finally
{
WriteToFile("QB Thread Finished " + DateTime.Now);
}
}
}
}
catch(Exception high)
{
WriteToFile(high.ToString());
}
// Hide
ShowWindow(handle, SW_HIDE);
//Console.Read();
}
private static void WriteToFile(string Message)
{
string filepath = "FILEPATH REMOVED FOR STACK OVERFLOW(unneeded information)" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
Upvotes: 0
Views: 491
Reputation: 18
Use event logs. navigate to Event Viewer>Window Logs>Application then look for your application on the last time it runs. you can see the what happen there.
Upvotes: -1