1

I am trying to make a connection to SAP GUI through C#. For reference, I am using code found here: How do I automate SAP GUI with c#

I have added a reference to sapfewse.ocx and the corresponding using statement. Here is my code:

using SAPFEWSELib;

private void OpenSAP()
{
    GuiApplication Application;
    GuiConnection Connection;
    GuiSession Session;
                        
    Application = (GuiApplication)System.Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1"));

    // How do I find the connection string that I use to connect to SAP?
    Connection = Application.OpenConnectionByConnectionString("XXXXXXXXX", false, true);
    Session = (GuiSession)Connection.Sessions.Item(0);

    Session.TestToolMode = 1;

    ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField")).Text = "XXX";
    ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField")).Text = "XXXXXXXX";
    ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField")).Text = "XXXXXXXX";
    ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField")).Text = "XX";            
}

I am getting an error when I run this code saying

...connection broken

How can I be sure of the correct connection string to use? Where can I find the connection string that I use to connect to SAP? Or is there a better way than using the OpenConnectionByConnectionString() method?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Sup3rUser
  • 21
  • 1
  • 1
  • 6

4 Answers4

4

Use this line of code: Connection = Application.OpenConnectionByConnectionString("/H/IP Address/S/3200", false, true);

H: Host IP Address S: Host Port Number. Default is 3200. Check yours.

3

Here is a part of the official documentation that describes the format of the route or connection string:

Definition

A route string describes the stations of a connection required between two hosts. A route string has the syntax

(/H/host/S/service/W/pass)*

It consists of any number of substrings in the form /H/host/S/service/W/pass.

Caution: H, S, and W must be uppercase!

Structure

A route string contains a substring for each SAProuter and for the target server. Each substring contains the information required by SAProuter to set up a connection in the route: the host name, the port name, and the password, if one was given.

Syntax for substrings:

  • /H/ indicates the host name. Note that the host name must be at least two characters long.
  • /S/ is used for specifying the service (port); it is an optional entry, the default value is 3299
  • /W/ indicates the password for the connection between the predecessor and successor on the route and is also optional (default is “”, no password)
vwegert
  • 18,371
  • 3
  • 37
  • 55
0

Your connection string is the name of the SAP area such as ERP, P56, or DFQ

Connection = Application.OpenConnectionByConnectionString("ERP", false, true);

Use that

Luke101
  • 63,072
  • 85
  • 231
  • 359
0

The "XXXXXXXXX" argument ("ConnectionString") is actually the description of the connection string that you can locate in the saplogon.ini file, which is at %AppData%\Roaming\SAP\Common