C# .NET - How to close opened QBW file in Quickbooks from integrated C# desktop application

Asked By Meena Trivedi on 14-Oct-13 10:42 AM
I had developed Quickbooks integration desktop application in c#. It is working fine with one QBW file which is opened in Quickbooks. But What I want is - Connecting one QBW file and then adding data to it and then close this QBW file and then connecting another QBW file and sending data to that.
I am getting, error when connecting to another QBW file - A QuickBooks company data file is already open and it is different from the one requested or there are multiple company files open.
Robbe Morris replied to Meena Trivedi on 14-Oct-13 10:53 AM
You'd have to post some of your code.  Something is not permanently closing the connection to the file.
Meena Trivedi replied to Robbe Morris on 15-Oct-13 02:18 AM
strCompanyFile = "D;/Test..QBW";
Item objAcc = new Item("US", 6, 0, strCompanyfile, "Test QB");                        

Item.sessionManager.OpenConnection(strCompanyfile, "Test QB");
Item.sessionManager.BeginSession(strCompanyfile, ENOpenMode.omDontCare);

then added Item to this company file and then

if (dsItem.Tables[0].Rows[i]["QBCompanyId"] != dsItem.Tables[0].Rows[i + 1]["QBCompanyId"])
 {
   Item.sessionManager.EndSession();
   Item.sessionManager.CloseConnection();
  }

This complete code is in loop and should work for next company as from database the next company is D:/ABC.QBW so the strCompanyFile will be "D:/ABC.QBW" and the same code will execute then.

But when I am runnign this code it is showing error that - A QuickBooks company data file is already open and it is different from the one requested or there are multiple company files open.
Robbe Morris replied to Meena Trivedi on 15-Oct-13 08:11 AM
This line never is never used?

Item objAcc = new Item("US", 6, 0, strCompanyfile, "Test QB");  

Item.sessionManager looks like it is static because I don't see where you create an instance of "Item".  Are you sure it should be like this?  I would expect database oriented classes to implement IDisposable so everything can be destroyed when the object is done with the connection.

Put a breakpoint in here:  I don't think it is "always" closing your session. 

if (dsItem.Tables[0].Rows[i]["QBCompanyId"] != dsItem.Tables[0].Rows[i + 1]["QBCompanyId"])
 {
   Item.sessionManager.EndSession();
   Item.sessionManager.CloseConnection();
  }           
Meena Trivedi replied to Robbe Morris on 15-Oct-13 08:18 AM
This code is in my Item class -

public Item(string country, short majorVersion, short minorVersion, string filePath, string applicationName)
      {
        if (country.Length > 0) Item.QBVersionn.country = country;
        if (majorVersion > 0) Item.QBVersionn.majorVersion = majorVersion;
        if (minorVersion > 0) Item.QBVersionn.minorVersion = minorVersion;
        sessionManager = null;
        sessionManager = new QBSessionManager();

        if (filePath.Length > 0 && File.Exists(filePath)) CompanyFilePath = filePath;
        if (filePath.Length > 0 && File.Exists(filePath)) this.logFilePath = filePath.Substring(0, filePath.LastIndexOf("\\") + 1) + "QBInterface.log";
        if (applicationName.Length > 0) ApplicationName = applicationName;
      }
Meena Trivedi replied to Meena Trivedi on 17-Oct-13 02:28 AM
Can anybody help me to resolve this issue