Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Close connection

Hi,

If I have a connection set up like this, how can I close it? There is no close or dispose in the application object.

try
{
      // This creates an Instance of the API. 
      VismaLön600.Application lon = new VismaLön600.Application();

      // Initialize the server
      // For Lön 2018.1 or later, use Init (with APIversion 2.0) instead of InitServer
      if (lon.Init(“2.0”, null) != 0)
      {
          return false;
      }

          // Login with a API-user. Username, password and the name of the company in the database
      string Login_Signature = "api";
      string Login_Pwd = "se";
      string DBCompanyName = "SPCS_Lön_Ftg1";

      int returncode = lon.Logon (Signature , Login_Pwd , DBCompanyName);

      if (returncode != 0)
      {
                  Console.WriteLine("Error when trying to login." + returncode);
                  return false;
      }

      // You are now logged in
      return true;
  }

       catch (System.Runtime.InteropServices.COMException ex)
       {
           // When something unexpected happens, like the database could not be Initialized,
           // then the API thows an exception.
           Console.WriteLine("COMException when trying to login." + ex.ErrorCode);
       }

Open in new window


When I read records and then try to read again I get an error that it's already logged on:
try
{
      // Creates a TableObject that contains the records of Employees
    EmployeeTableObject employeeTableObject = lon.CreateTableObject((int)Tables.Table_Employee);

    // Reads all the Records Id and put them in the employeeTableObject              
    employeeTableObject.Fetch();

    // IsEOF is true as long as the position in the table is inside the boundaries.
    while (!employeeTableObject.IsEOF())
    {
            // Reads the current record.
            String firstname = employeeTableObject.GetString((int)Employee_Properties.EMP_FirstName);
            Console.WriteLine(firstname);

          // When moving to a new record, the record is read into the current record.
            TestMain.EmployeeTO.MoveNext();
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
            Console.WriteLine("COMException:" + ex.ErrorCode);
    }
}

Open in new window


Thanks for help!

Peter
Avatar of louisfr
louisfr

Do you have any documentation on that API?
Avatar of Peter Nordberg

ASKER

Here it is!

Thanks for help!

Peter
Opps, forgot to upload attachement.
Lon_Api_2_0.rar
There is usually a logout method when there is a logon.

lon.Logon (:...,..)-  //connect
Lon.Logout()   // disconnec


"When I read records and then try to read again I get an error that it's already logged on:"
Maybe un
lon.CreateTableObject((int)Tables.Table_Employee) you  call again Logon
That's the problem. There is no logout method connected to that object.
I looked at the documentation and it doesn't provide a lot of information.
Only solution I can think of is to keep track of if you're logged in or not, of which tables you have already fetched, use MoveFirst if you need to read it again. It should log off when the program terminates.
Application has finalize method:


you must put the varible Application in the scope of the task you want to perform so that when you leave the scope of the task will automatically call the method finalize and release the connection and associated resources. You should not use it as a global variable because in that case you would only call the finalized method when the application ends. You can pass it as a parameter to modularize your program.


MainProcess
     {

          Int rt =ProcessSPC(signatura,pe, company1)
rt =ProcessSPC(signatura,pe, company2)
 

    }
Int ProcesSPC(SIG,PW,com) {
   Aplication lon....
   Initialize
  Logon

 ProcessRecords(lon)

Return errcode;


} // un this Point conectio  close o free resourcw
@jcgd The Finalize method won't be called until the application ends, even if you only store the object in a local variable, unless there is a reason for a garbage collection to occur.
That's right,  but this instantes will destroy  next GC run and not necesary oscursat the end of program. Before exit the scoped set to null.

Second call to the method create new instance
I would also try doing an invalid login with an incorrect username and password dame company.  Perhaps this forces the active seaion to disconnect.
You can force the garbage collection using GC.Collect() and wait for execution of the finalizer with GC.WaitForPendingFinalizers().
I don't usually advise tampering with the GC, but it's unusual circumstances.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.