Skip to Content
avatar image
Former Member

Connection of companies in loop

Hi all,

i have a problem regarding multiple connection to companies....here iam able to connect to multiple companies individually by one by one(by static declaration)..iam using below code.....

 oCompany.CompanyDB = Combo1.Text
        oCompany.UserName = Text1.Text
        oCompany.Password = Text2.Text
 
        lRetCode = oCompany.Connect
   
        oItem.ItemCode="I0001"
        oItem.ItemName=Inventory1 
        oItem.Add()
  
       oCompany1.CompanyDB = Combo2.Text
        oCompany1.UserName = Text3.Text
        oCompany1.Password = Text4.Text
 
        lRetCode = oCompany1.Connect

       oItem.ItemCode="I0002"
        oItem.ItemName=Inventory2 
        oItem.Add()
 
       oCompany2.CompanyDB = Combo3.Text
        oCompany2.UserName = Text5.Text
        oCompany2.Password = Text6.Text
 
        lRetCode = oCompany2.Connect

        oItem.ItemCode="I0003"
        oItem.ItemName=Inventory3 
        oItem.Add()

but by the above coding i can't connect to 'n' no.of companies during run-time...the above given is static testing.....but i want to create dynamically(creating multiple connections at runtime & adding different items).....can anybody suggest me some ideas????

regards,

shangai.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Sep 08, 2008 at 02:17 PM

    Shanghai,

    i recommend you to implement in the loop that you

    1. disconnect the company after use with oCompany.Disconnect and than recreate the company object.

    than one object is enough (it's slower but it works)

    2. take care that the same user is not connected more than twice

    (an User usually can only login twice in business one)

    lg David

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      ..here is my idea as a quick and dirty example. The list of connected companys is stored in a hashtable:

      Global Multi-Company-List:

          Public Shared MyCompanies As New System.Collections.Hashtable ' GLOBAL(!) Class-Var
      

      Multi-Company-Functions:

          Public Shared Function MultiConnect(ByVal CmpDB As String, ByVal User As String, ByVal PW As String) As Boolean
              Dim oCompany As New SAPbobsCOM.Company
              Dim retVal As Integer
      
              oCompany.Server = "YOURSERVER" ' should also be a param. of this function
              oCompany.DbUserName = "YOURDBUSER" ' should also be a param. of this function
              oCompany.DbPassword = "YOURDBPW" '  "    "  "  "   "...
              oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2005
              oCompany.CompanyDB = CmpDB
              oCompany.UserName = User
              oCompany.Password = PW
      
              retVal = oCompany.Connect
      
              If retVal = 0 Then
                  If MyCompanies.ContainsKey(CmpDB) Then
                      If MyCompanies.Item(CType(CmpDB, SAPbobsCOM.Company)).Connected Then
                          MsgBox("Already connected to " & CmpDB)
                          oCompany.Disconnect()
                          Return True
                      Else
                          ' TODO!! (reconnect or whatever...)
                      End If
                  Else
                      MyCompanies.Add(CmpDB, oCompany)
                      Return True
                  End If
              Else
                  MsgBox(oCompany.GetLastErrorDescription)
                  Return False
              End If
          End Function
      
          ' Getting a specific Company of the MultiCompany-List
          Public Shared Function GetCmp(ByVal CmpDB As String) As SAPbobsCOM.Company
      
              If MyCompanies.ContainsKey(CmpDB) Then
                  Return CType(MyCompanies.Item(CmpDB), SAPbobsCOM.Company)
              Else
                  Throw New Exception("Not connected to " & CmpDB)
              End If
          End Function
      

      Testrun:

                  MultiConnect("DB1", "manager", "manager")
                  MultiConnect("DB2", "manager", "manager")
                  MultiConnect("DB3", "manager", "manager")
      
                  MsgBox(GetCmp("DB1").CompanyName)
                  MsgBox(GetCmp("DB2").CompanyName)
                  MsgBox(GetCmp("DB3").CompanyName)