Thursday 29 July 2021

Connecting to sap business one from c#

What do you need to do in order to connect to sap business one from c#?

First of all you need to know which version of the API do you want to use and does API is 32bit or 64bit. 

The API is called "DI API" and can be found on the same server where you did the SAP installation in the same directory.

For example, DI API version 100 is located in


Before you actually can use the API you need to register the SAP Com object. Inside "DI API" directory you will find a file "SAPbobsCOM100.dll" (for version 100) and register it with 

%systemroot%\System32\regsvr32.exe C:\DI API 100\SAPbobsCOM100.dll

Note that we use "regsrv32.exe" is used for the 64bit version.

After COM object is installed you can add it from Visual Studio "Add references"
Add COM object as reference "SapBob"

 Once we have the COM object we can start coding. A basic object which is used for connection is called "Company"


  static void Main(string[] args)
        {
            Company oCompany = new Company();
            oCompany.Server = "Sap-10-Sql";
            oCompany.language = BoSuppLangs.ln_English;
            oCompany.DbServerType = BoDataServerTypes.dst_MSSQL2019;
            oCompany.CompanyDB = "MyCompanyDb";
            oCompany.UserName = "admin";
            oCompany.Password = "pass";
            oCompany.DbUserName = "sa";
            oCompany.DbPassword = "pass";
            oCompany.UseTrusted = false;
        
            var lRetCode = oCompany.Connect();
            if (lRetCode == 0)
                Console.WriteLine("Done");
            string sErrMsg = string.Empty;
            oCompany.GetLastError(out lRetCode, out sErrMsg);
            Console.WriteLine(sErrMsg);
            oCompany.Disconnect();

        }

"Server" is the DB server and not an application server.
"CompanyDB" is the name of the database of the database server.
"UseTrusted" controls if DB authentication using DB credential or Windows credentials.

Now if you think everything will work after this, you are wrong :)
Probably you will get the following error
Failed to connect SLD, make sure SLD server is correct and available

This is because you also need to define the URL for SLD server (have no idea what is it, but you need it) and the license server.

You can do it programmatically by setting



oCompany.LicenseServer = "sap-10-app:40000";
oCompany.SLDServer = "sap-10-app:30010";

or you can do it in the conf file. In the same location where "DI API 100" folder is located, search for the folder called "Conf". Inside the conf directory open the file b1-local-machine.xml and modify the following values



Now you are good to go!

No comments:

Post a Comment