Hi all.
Can anyone provide me with an example on how to handle the Company Changed event?
What I do now is something like this (the SetApplication works when I connect in the first place):
Dim sConnectionString As String
Dim SboGuiApi As SAPbouiCOM.SboGuiApi
Private Sub con_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent
Select Case EventType
Case SAPbouiCOM.BoAppEventTypes.aet_ShutDown
End
Case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged
vcmp = Nothing
SetApplication()
End Select
End Sub
Sub SetApplication()
Dim str as string
SboGuiApi.Connect(sConnectionString)
SBO_Application = SboGuiApi.GetApplication()
vcmp = New SAPbobsCOM.Company()
str = vcmp.GetContextCookie()
str = SBO_Application.Company.GetConnectionContext(str)
vcmp.SetSboLoginContext(str)
vcmp.Connect()
. . .
end Sub
The problem is, that I keep getting the CompanyChanged event. My guess is that it is because I reconnect to the application before the eventhandler has returned, but I can't really figure out how to do....
Any help appriciated.
Thanks in advance
Jesper Carstensen
Hi Jesper
We did it like that and it is working well.
Special hint: our procedure "RunSboChecked"; it returns a catchable VB.NET error in place of a non-catchable SBO error value.
Private Sub mwuiApp_AppEvent( _
ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles mwuiApp.AppEvent
Select Case EventType
Case SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition, _
SAPbouiCOM.BoAppEventTypes.aet_ShutDown
Me.mwuiApp = Nothing
Me.muiApi = Nothing
Me.mdiCmp = Nothing
GC.Collect()
Application.Exit()
End
Case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged
Me.mdiCmp = Nothing
Me.mdiCmp = New SAPbobsCOM.Company
Call Me.subConnectToDI()
End Select
End Sub
Public Sub subConnectToDI()
Call Me.subSetConnectionContext()
Call Me.subConnectToCompany()
mstrServer = mdiCmp.Server
mstrCompanyDB = mdiCmp.CompanyDB
mstrSboUser = mdiCmp.DbUserName
End Sub
Private Sub subSetConnectionContext()
Dim strCookie As String
Dim strConnectionContext As String
Try
'// Acquire the connection context cookie from the DI API.
strCookie = mdiCmp.GetContextCookie
'// Retrieve the connection context string from the UI API using the
'// acquired cookie.
strConnectionContext = mwuiApp.Company.GetConnectionContext(strCookie)
'// Set the connection context information to the DI API.
RunSboChecked(mdiCmp.SetSboLoginContext(strConnectionContext))
Catch ex As Exception
Call subBeenden(ex, "Die zur Verbindung mit der Datenbank nötigen Informationen konnten " _
& "nicht ermittelt werden.")
End Try
End Sub
Private Sub subConnectToCompany()
Try
'// Establish the connection to the company database.
RunSboChecked(mdiCmp.Connect)
Catch ex As Exception
Call subBeenden(ex, "Die Verbindung zur Datenbank konnte nicht hergestellt werden.")
End Try
End Sub
Public Sub RunSboChecked(ByVal intSboReturnValue As Integer)
If intSboReturnValue = 0 Then
'everything ok, no SBO error
Else
Dim intError As Integer
Dim strError As String
mdiCmp.GetLastError(intError, strError)
strError = "SBO error " & CStr(intError) & ": " & strError
Throw New Exception(strError)
End If
End Sub
Add a comment