Reputation: 11
I want to save different SAP users to a database and check with a SAP login if the input was correct. For the login, I have to use the SAP.net Connector3.0.
Procedure: click button -> enter user1 -> SAP login -> if correct: save, else errormessage -> SAP logout -> click button again-> enter user2 -> Sap login ->...and so on
My code:
Private Sub ButtonSaveUser_Click(sender As Object, e As EventArgs) Handles ButtonSaveUser.Click
Try
Dim parms As New RfcConfigParameters
parms.Add(RfcConfigParameters.User, TextBoxUser.Text)
parms.Add(RfcConfigParameters.AppServerHost, TextBoxRouter.Text)
parms.Add(RfcConfigParameters.SystemNumber, TextBoxSystemnumber.Text)
parms.Add(RfcConfigParameters.SystemID, TextBoxSystem.Text)
parms.Add(RfcConfigParameters.Password, TextBoxPasswort.Text)
parms.Add(RfcConfigParameters.Client, TextBoxClient.Text)
parms.Add(RfcConfigParameters.Language, TextBoxLanguage.Text)
parms.Add(RfcConfigParameters.Name, "Connection Name")
Dim dest As RfcDestination = RfcDestinationManager.GetDestination(parms)
Dim repo As RfcRepository = dest.Repository
' in this space is the part where I save the user information. this works.
Catch ex As Exception
MsgBox("Logon failed. " + ex.Message)
End Try
End Sub
Question: What do I have to write at the end of my code to logout or disconnect? I thought that there might be something like a 'dest.disconnect' command, but i can't find such a command.
(My problem is, that I'm new at programming and don't know how to implement the missing parts.)
If anybody could post a vb code or give any other information, that would be awesome! Thanks :)
Upvotes: 1
Views: 4201
Reputation: 11
I solved my problem with the following code:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports SAP.Middleware.Connector
Public Class Form
Public Sub Buttonsaveuser_Click(sender As Object, e As EventArgs) Handles Buttonsaveuser.Click
Dim objDestConfig As New InMemoryDestinationConfiguration
Try
Dim parms As New RfcConfigParameters
parms(RfcConfigParameters.User) = TextBoxUser.Text
parms(RfcConfigParameters.AppServerHost) = TextBoxRouter.Text
parms(RfcConfigParameters.SystemNumber) = TextBoxSystemnumber.Text
parms(RfcConfigParameters.SystemID) = TextBoxSystem.Text
parms(RfcConfigParameters.Password) = TextBoxPasswort.Text
parms(RfcConfigParameters.Client) = TextBoxClient.Text
parms(RfcConfigParameters.Language) = TextBoxLanguage.Text
parms(RfcConfigParameters.Name) = TextBoxDestination.Text
RfcDestinationManager.RegisterDestinationConfiguration(objDestConfig)
objDestConfig.AddOrEditDestination(parms)
Dim destination1 As RfcDestination = RfcDestinationManager.GetDestination(TextBoxDestination.Text)
destination1.Ping()
Catch ex As Exception
MsgBox("Logon failed. " + ex.Message)
Finally
RfcDestinationManager.UnregisterDestinationConfiguration(objDestConfig)
objDestConfig.RemoveDestination(TextBoxDestiantion.Text)
End Try
End Sub
End Class
Public Class InMemoryDestinationConfiguration
Implements IDestinationConfiguration
Private availableDestinations As Dictionary(Of String, RfcConfigParameters)
Public Sub New()
availableDestinations = New Dictionary(Of String, RfcConfigParameters)()
End Sub
Public Function GetParameters(ByVal destinationName As String) As RfcConfigParameters _
Implements IDestinationConfiguration.GetParameters
Dim foundDestination As RfcConfigParameters = Nothing
availableDestinations.TryGetValue(destinationName, foundDestination)
Return foundDestination
End Function
Public Function ChangeEventsSupported() As Boolean _
Implements IDestinationConfiguration.ChangeEventsSupported
Return True
End Function
Public Event ConfigurationChanged As RfcDestinationManager.ConfigurationChangeHandler _
Implements IDestinationConfiguration.ConfigurationChanged
Public Sub AddOrEditDestination(ByVal parameters As RfcConfigParameters)
Dim name As String = parameters(RfcConfigParameters.Name)
If availableDestinations.ContainsKey(name) Then
Dim EventArgs As New RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters)
RaiseEvent ConfigurationChanged(name, EventArgs)
End If
availableDestinations(name) = parameters
Dim tmp As String = "Application server"
Dim isLoadValancing As Boolean = parameters.TryGetValue(RfcConfigParameters.LogonGroup, tmp)
If isLoadValancing Then
tmp = "Load balancing"
End If
End Sub
Public Sub RemoveDestination(ByVal name As String)
If availableDestinations.Remove(name) Then
RaiseEvent ConfigurationChanged(name, New RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED))
End If
End Sub
end class
Upvotes: 0
Reputation: 11338
I have never bothered. I let the Destination manager manage the connections. but there is an option to unregister destinations. You could trying that
public static void UnregisterDestinationConfiguration(IDestinationConfiguration config)
but i was under the impression the session manager should manage things eg:
EDITED to reflect comments
var parms = new RfcConfigParameters();
// the connection params are set .....
//parms.xxx =
RfcDestination _dest;
_dest = RfcDestinationManager.GetDestination(parms);
IRfcFunction func = _dest.Repository.CreateFunction("Z_MY FUNC");
RfcSessionManager.BeginContext(_dest);
func.Invoke(_dest);
RfcSessionManager.EndContext(_dest);
my point was more of contrasting logon version a destination that the Session manager managers for you.
Upvotes: 1