Aug 16

Build an RFC Client with NCo 3.0 – A Step-By-Step Guide

The SAP .Net Connector 3.0 (NCo 3.0) offers many improvements over the 2.0 version of that product. Unfortunately, SAP no longer offers example .NET code.  This blog attempts to fill that gap by describing how to build a simple RFC Client using SAP .Net Connector (NCo) 3.0.

The sample program displays details about companies defined by SAP. There are two BAPI calls involved, BAPI_COMPANY_GETLIST and BAPI_COMPANY_GETDETAIL.
Along with the SAP .Net Connector 3.0, we are using Microsoft Visual Studio 2010 and the Microsoft .Net Framework 4.0 to build our sample.  Prior to starting, you will have to download and install NCo 3.0 (OSS login required).

Setting up the Project

Using Visual Studio 2010, create a new Windows Form project. In the project properties, be sure to set the Target Framework to .Net Framework 4.0.

Add references to the SAP .Net Connector 3.0. There are two DLLs, sapnco.dll and sapnco_utils.dll.

Design the Form

Add two controls to the form. The first is a List Box, which will contain the list of available SAP companies. The second control to add is a Property Grid control, which is used to display the details of a particular SAP company when the user selects one from the List Box.

Using App.Config to define the SAP Connections

There are several methods you can use in your solutions to define a particular SAP host. For this example, we are using an app.config file to define our SAP host.
The SAP .Net Connector 3.0 includes sample app.config files. For the RFC client, be sure there is a sectionGroup definition for <ClientSettings>.
The SAP host is defined by the destinations section. Within the destination, the Name field identifies this app.config entry to our program code. Specify the appropriate user name, password, SAP host name, client and system number in the destination section.

The BAPIs

Use the SAP transaction SE37 to determine the parameters for functions BAPI_COMPANY_GETLIST and BAPI_COMPANY_GETDETAIL. This will tell us what parameters need to be passed in and where to find the results that are returned to our code.

The Code

During the form load event, we want to populate our List Box with the defined companies from SAP.

  1. Acquire a valid RfcDestination to use. Use method RfcDestinationManager.GetDestination() to do this. GetDestination() requires a parameter to determine which SAP host to refer to. Pass in the Name of the SAP host you previously defined in the app.config file. (Line 60)
  2. Now that we have a valid RfcDestination object, we need access to the SAP Repository. The repository contains information about the BAPI calls we are going to make. The RfcRepository is an attribute of an RfcDestination object. (Line 62)
  3. Using the RfcRepository object, acquire a reference to the SAP BAPI by calling method RfcRepository.CreateFunction() method. Pass in the name of the desired function as a string parameter to CreateFunction(). This method returns an object we can use to setup parameters, invoke the function, and retrieve results.
  4. Now we can make the RFC calls into SAP. Using the IRfcFunction object returned by CreateFunction(), call the Invoke() method, passing in our RfcDestination object as parameter. This will make the RFC call into SAP, and provides the results via our IRfcFunction object. (Line 68)
  5. SAP function BAPI_GET_COMPANYLIST returns a table of company records. We use an instance of an IRfcTable object that we retrieve by calling the GetTable() method on the IRfcFunction object. To get a desired table, pass in the table name to the GetTable() method. (Line 70)
  6. We now have an instance of an IRfcTable object that contains company information through which we can iterate. It is possible to iterate through the data by setting the CurrentIndex property on the returned table. For a particular row in the table, it is possible to access values using the GetString() method. (Lines 75-79)
  7. For each company, get the CompanyNumber, and use that as a parameter to SAP function BAPI_COMPANY_GETDETAIL. BAPI_COMPANY_GETDETAIL returns detailed information about that particular SAP company. The techniques are the same as before, with the additional step of using the SetValue() method on the IRfcFunction object to pass in the company number as a parameter. (Lines 78-84)
  8. BAPI_COMPANY_GETDETAIL returns data as an instance of the IRfcStructure interface. Access individual field data of the company record, using the GetString() method of the IRfcStructure object instance. (Lines 88-99)
  9. Extract the company details as desired. Add them to some object you create and then add that object to the List Box control. (Line 101)

The rest of the code is regular Windows Forms programming. Your code will need to wire up the List Box changed events so that the Property Grid will display the details of the selected company.

Additional Information

Avatar

About The Author

Former Partner and Senior Consultant at DataXstream, Craig Stasila played a key role in developing DataXstream. For current information regarding Craig Stasila please see his Linkedin profile.

30 Comments

  1. […] sample projects that were instrumental in my learning process.  As I mentioned in my post detailing the steps to build an RFC client, SAP no longer supplies code samples with SAP .NET Connector (NCo) 3.0.  So, I decided to make […]

  2. […] Build an RFC Client with NCo 3.0 – A Step-By-Step Guide […]

  3. […] Build an RFC Client with NCo 3.0 – A Step-By-Step Guide […]

  4. Avatar
    Avendra
    November 8, 2011 at 4:12 am · Reply

    Hi. Do have an example for this but using a web project instead of a windows app. And how would you go about setting the destinations in the web.config file. Thanks in advance

    • Avatar
      Craig Stasila
      November 8, 2011 at 8:14 am · Reply

      Avendra, I will try to create a web project example and post it in the next couple of weeks. Please check back.

  5. […] I published an article showing step-by-step instructions for how to consume an SAP RFC with the SAP .Net Connector 3.0 (NCo 3.0).  The article included code samples and a working code example for download written in C#.  Well, […]

  6. Avatar
    Mon
    November 11, 2011 at 8:28 am · Reply

    Thank for your solution.

    I used this sample for developed a windows application that work, but not work in web application.
    “The type initializer for ‘SAP.Middleware.Connector.RfcDestinationManager’ threw an exception.”

    Please advice me,
    Best Regards,

  7. Avatar
    Dan Hibbert
    November 11, 2011 at 4:49 pm · Reply

    Hi,
    Thanks for this informative post – very useful !
    We’ve started trying to so single sign on using SNC and there starts the beginning of the trouble – the SAP ops team have set up the certificates and everything looks like its connecting – they can see the connection coming in but it’s saying not enough log on data. Checking SM50 shows a connection being made but no user details being passed. In the old .net connector days, I would have used EXTIDTYPE and EXTIDDATA to pass in the data but I don’t see anything in the new version that allows me to set those parameters.

    Do you have any advice or tips on this please?

    Thanks
    Dan

  8. Avatar
    HiAde
    November 19, 2011 at 4:25 pm · Reply

    problem in NCO 3.0.3.0 convert time
    Details
    http://forums.sdn.sap.com/message.jspa?messageID=10830236#10830236

  9. Avatar
    Anooj
    March 15, 2012 at 5:30 am · Reply

    How can pass Table parameters with NCo 3.0 ?We have many such BAPI’s.

  10. […] Since I posted step-by-step instructions on how to build an NCo RFC client, the  request I most often get is how to populate and pass a table of data as a parameter to an SAP RFC.  In this blog, I will walk through the steps to pass a table parameter to from a .NET program to SAP via NCo 3.0.  I will not be covering the basics of how to set up NCo 3.0 as a RFC client as I have already covered that. […]

  11. Avatar
    leandro
    October 16, 2012 at 2:53 pm · Reply

    E se eu quiser altera o “PASSWD” por exemplo??

    • Avatar
      Craig Stasila
      October 16, 2012 at 4:53 pm · Reply

      A senha é armazenada no arquivo de configuração. Mudá-lo à vontade.

  12. Avatar
    Amie
    November 20, 2012 at 1:17 pm · Reply

    I also had the same problem SAP.Middleware.Connector.RfcDestinationManager thew an error. What was the solution to this? I have made sure that the .NET Framework 4 is targeted, downloaded your whole project, and it doesn’t work.

  13. Avatar
    Andrew
    December 14, 2012 at 1:26 pm · Reply

    Hello there guys! Tks for the terrific post!

    I`m trying to connect and getting the following error message. Can you please help me?

    LOCATION CPIC (TCP/IP) on local host with Unicode
    ERROR partner ‘10.17.225.163:3300’ not reached
    TIME Fri Dec 14 16:25:21 2012
    RELEASE 720
    COMPONENT NI (network interface)
    VERSION 40
    RC -10
    MODULE nixxi.cpp
    LINE 3286
    DETAIL NiPConnect2: 10.17.225.163:3300
    SYSTEM CALL connect
    ERRNO 10061
    ERRNO TEXT WSAECONNREFUSED: Connection refused
    COUNTER 2

  14. Avatar
    Sean
    February 4, 2013 at 1:53 pm · Reply

    Have you been able to get this to run as a web application? I can make it work in web forms and as a console app with no problems, but, that same code in a web app fails with the error: Could not load file or assembly ‘sapnco’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

    I tried building targeting 32 bit and 64 bit with no luck and running under IIS in classic mode with the enable 32 bit apps checked on the pool with no luck.

    Would be nice to know if someone got it running in a web environment. All I’ve found is people have run into the same issue with no answers.

    Thanks.

    Sean

  15. Avatar
    bubu
    March 12, 2013 at 4:45 am · Reply

    Hi,

    Thanks for this Guide, but I always get this error message:

    Cannot get destination D01DBCI — no destination configuration registered

    Copying the .dll into my project did not solve the problem.

  16. Avatar
    smoula
    May 9, 2013 at 8:16 am · Reply

    Hi,

    Is there somebody who know give me advice why this part code
    RfcRepository rfcRep = rfcDest.Repository;
    giving exception error?

    LOCATION CPIC (TCP/IP) on local host with Unicode
    ERROR timeout occured
    TIME Thu May 09 14:06:10 2013
    RELEASE 720
    COMPONENT NI (network interface)
    VERSION 40
    RC -5
    MODULE gwxx.c
    LINE 646
    DETAIL timeout 60000 msecs
    SYSTEM CALL GwRead
    COUNTER 2

  17. Avatar
    Juan Carlos
    May 28, 2013 at 8:25 pm · Reply

    Hi Craig,

    I have been using the SAP NCO 3.0 in my WCF Project and everything was ok at my development and testing environment.

    But, at the production environment I was getting an error like this:

    * Cannot get a client connection: invalid destination PRD (REPLACED)

    I saw that after the process at SAP, the connection was alive. And I added two sentences to close the connections:

    * RfcDestinationManager.RegisterDestinationConfiguration(sap); Just before of BeginContext
    * RfcDestinationManager.UnregisterDestinationConfiguration(sap); Just after of EndContext

    And now, I am getting this error:

    * Hard-coded logon parameters not allowed when using a DestinationConfiguration

    I replicated this at my development environment and it happens when I send requests in parallel to the WCF Service, but I don’t know hot to correct it.

    Do you know what the problem is with this?

    Thanks in advance,

    Regards.

  18. Avatar
    Nacho
    November 8, 2013 at 7:14 am · Reply

    Hello,

    I’ve tried your option, but I’m receiving the same error.

    You can look at this link:
    http://stackoverflow.com/questions/19857696/could-not-load-file-or-assembly-sapnco-or-one-of-its-dependencies-se-ha-inten

    Regards,

  19. Avatar
    smoula99
    December 10, 2013 at 8:57 am · Reply

    Hi

    Can you somebody help me, how can I update Vendors data in SAP using RFC?

    Thank you

  20. Avatar
    smoula99
    January 2, 2014 at 8:16 am · Reply

    Hi

    Is there somebody who can help me to update vendor data using net. connector with RFC function (or some another way to update vendors data from 3th party program).

    Thank you

  21. Avatar
    Gyan
    January 14, 2014 at 6:32 am · Reply

    Hi I have downloaded your code , while running i am getting below error at line

    RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination(“Dev”);

    “Cannot get destination Dev — no destination configuration registered”

  22. Avatar
    Ton Slijpen
    January 16, 2014 at 12:28 pm · Reply

    Hi Craig

    Could you please tell me how I need to read a CLUSTER in SAP (with vb.net)

    For tables I use the RFC_READ_TABLE bapi and this works fine.

    I cannot manage to read tables in a cluster (T588M, Q588M)

    Thanks

    Ton

  23. Avatar
    Zaigham
    March 5, 2014 at 12:19 am · Reply

    Thanks Craig,

    You have made my Day.
    Really helpful articles you have posted related to NCO 3.0
    please allow me to make some addition to it, the documentation provided with this component are really helpful and needed to go through, as most of the problems related to versions of .Net framework, NCO 3.0 on 32bit/64bit and most important the version of CLR and their combinations are most important.

    regards,

    Zaigham

  24. Avatar
    bhavesh
    February 22, 2016 at 2:46 am · Reply

    i have done same to same apply code in vb.net in that case my code is working fine in SAP+Oracle database, but after some time SAP + HANA(new database) is update after that my code was not working as per below i getting error please help me…
    SAP.Middleware.Connector.RfcCommunicationException was caught
    HResult=-2146232832
    Message=
    LOCATION CPIC (TCP/IP) on local host with Unicode
    ERROR
    partner ‘192.168.52.35:3300’ not reached
    TIME
    Mon Feb 22 09:43:59 2016

    RELEASE 720

    COMPONENT NI (network interface)

    VERSION 40

    RC -10

    MODULE nixxi.cpp

    LINE 3286

    DETAIL NiPConnect2: 192.168.52.35:3300

    SYSTEM CALL connect

    ERRNO 10061

    ERRNO TEXT WSAECONNREFUSED: Connection refused

    COUNTER 1

    Source=sapnco_utils
    StackTrace:
    at SAP.Middleware.Connector.CpicConnection.ConnectAsClient(Dictionary`2 options)
    at SAP.Middleware.Connector.RfcConnection.ConnectAsClient(RfcConfigParameters options)
    at SAP.Middleware.Connector.RfcConnectionPool..ctor(RfcDestination d, Boolean forRepositoryCalls, String uniqueKey)
    at SAP.Middleware.Connector.RfcConnectionManager.GetClient(RfcDestination destination, Boolean forRepository)
    at SAP.Middleware.Connector.RfcDestination.GetClient(Boolean forRepository)
    at SAP.Middleware.Connector.RfcDestination.SetAttributeValues(RfcSystemAttributes attribs)
    at SAP.Middleware.Connector.RfcDestination.get_SystemAttributes()
    at SAP.Middleware.Connector.RfcDestination.get_Repository()
    at Sap_Intigration.RadForm1.TransferToSAP() in D:\Developer\Bhavesh\Backup\Backup\20160222\Sap_Intigration\Sap_Intigration\SAP\RadForm1.vb:line 28
    InnerException:

  25. Avatar
    SujilKumar
    July 16, 2016 at 4:55 am · Reply

    HI , Thanks for the information its very helpful,

    But could not download the Source file.please help us to download the Source file

  26. Avatar
    Xaim Raza
    October 25, 2017 at 2:00 am · Reply

    getting Error on Company, not defined…

    regards

    Xaim Raza

  27. Avatar
    bouchaib
    April 25, 2018 at 10:31 am · Reply

    it is very helpful. Anyone pl share the source code to download

Leave a reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.