Nov 08

Build an RFC Client with NCo 3.0 for VB.NET – A Step-By-Step Guide

Recently, 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, not everybody writes in C#.  There are some of you that would prefer to write in VB.NET.  So, I have heeded your call, all you VB.NET developers!  Here is your very own article detailing how to build an RFC client with NCo 3.0 for VB.NET.

The SAP .Net Connector 3.0 (NCo 3.0) offers many improvements over SAP .NET Connector 2.0. 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 with VB.NET.  Click here to request a .zip file containing a copy of the source code.

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.  To change the target .NET framework version on a VB.NET project, first open the project’s property page.  Click on the “Compile” tab in the property page.  Then click on the “Advanced Compile Options..” button.

In the ensuing dialog window, change the target framework version.

Next, 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 39)
  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 42)
  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 48)
  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 50)
  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 55-59)
  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 60-67)
  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 67-78)
  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 80)

 

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

Follow these links for more information about NCo 3.0 programming:

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.

21 Comments

  1. Avatar
    Jim
    November 8, 2011 at 12:33 pm · Reply

    Incredible !
    this works fine
    it’s very easy too

  2. Avatar
    Ram
    February 5, 2012 at 10:05 pm · Reply

    Hi Criag,

    I am actually making a RFC call with parameters. How to add the parameters to RFC CALL? Please explain.

  3. Avatar
    Prashant
    February 10, 2012 at 7:16 am · Reply

    TO me its giving error, RFCDestinationManager type initializer error

  4. Avatar
    Prashant
    February 10, 2012 at 7:18 am · Reply

    It gives RFCDestinationManager error

  5. Avatar
    Tiny
    August 23, 2012 at 5:27 am · Reply

    Great docu for starters on NCo and even VB.
    No problems found following the steps listed, just have to look carefully for own typo’s when having problems.

  6. Avatar
    Bryan
    September 13, 2012 at 12:30 pm · Reply

    Thank you so much for the info you have provided. It is really tough to find ANY examples of VS2010 VB connecting to SAP. I’ve tried the example code you provided, but unfortunately, I am still getting the same error message I received when using the SAP .Net connector code example.

    I set up my destination configuration in the app.config file, but on the initial call to the RfcDestinationManager.GetDestination using the name from the app.config file, I get the error message: “Cannot get destination [mydestname] — no destination configuration registered.”

    I have tried looking in SM59 on our SAP system but I don’t know where/how to configure the connection. Should it be an ABAP connection, a TCP/IP connection, or something else somewhere else entirely? Could you please point me in the right direction to get this connection working?

    Any assistance you could provide would be greatly appreciated!!

  7. Avatar
    Bryan
    September 20, 2012 at 9:02 pm · Reply

    That did it. You guys are great! Thanks again!

  8. Avatar
    Ank
    September 27, 2012 at 8:41 am · Reply

    Hi Craig,

    Thank you for the perfect explenation in details. It’s working fine for our project with VS 2010.
    Now we need to convert the solution in to the VS 2008 version. I’m aware I cannot use the same .dll files/nco 3.0 for VS 2008, isn’t it? Do you have any tips which connector do I have to use for the same connection using Visual Studio 2008?

    Thank you very much in advance.

    regards,
    Ank

    • Avatar
      Craig Stasila
      September 27, 2012 at 10:55 am · Reply

      Ank,

      To my knowledge, you CAN use NCo 3.0 with VS 2008 as long as you target the entire project to .NET Framework 2.0 and use the NCo 3.0 DLL that is compiled with .NET Framework 2.0.

      Craig

  9. Avatar
    Slavko
    February 13, 2013 at 2:57 am · Reply

    Hi,

    during the start of thesolution with sample, i am receiving error:
    A first chance exception of type ‘System.TypeInitializationException’ occurred in DataXstream_NCo_3.0_RFC_Client_Sample_VB.exe.

    What is the reason for error? I filled app.config with data for our development server.

    Thanks, Slavko

  10. Avatar
    Slavko
    February 15, 2013 at 3:39 am · Reply

    Hi again,

    i found out, what was the problem-version of sapnco was for older framework, not for 4.0. Now it is working. I find this connector to be EXTREMLY useful!!!

  11. Avatar
    Jaime Romero
    March 29, 2013 at 12:46 pm · Reply

    Hi Craig, I can’t figure out how to pass a specific parameter to an RFC function I need to call from VB NET, the parameter in SAP is TBL1024 type, I have no problems connecting to the SAP system nor setting other parameters values nor calling other functions but this specific one is giving me a data type error.

    Any idease would be extremely helpfull I’m stuck 🙁

    Thanks.

  12. Avatar
    Jeff
    April 10, 2013 at 12:29 am · Reply

    Hi, i am trying to send data to SAP using RFC and dot net conn 3.0. The data pass to RFC table is correct but it return exception CNCL_ERROR raised.
    Any ideas to help me solve this?

    • Avatar
      Craig Stasila
      April 10, 2013 at 8:28 am · Reply

      What is the name of the RFC you are trying to call? It appears that the RFC you are calling is throwing the error you are receiving. Try logging in to SAP. Execute transaction SE37 and test the function with the data you are providing to see if you get the same error.

  13. Avatar
    Zaigham
    February 23, 2014 at 2:29 pm · Reply

    its really helpful, but
    Could you please inform me, how can I update or transform my old proxy based webservices made with NCO 2.0 on .net framework 1.0 with this new NCO 3.0 and framework 3.5/4.0

    its really Urgent, looking for a example project for same

  14. Avatar
    Martin
    July 16, 2014 at 5:06 pm · Reply

    Hello Craig:
    Thanks so much. Your articles are very good.

    I have a problem inserting data in a table from RFC called ZFMFI_2254
    There are 2 tables T_HEADER y T_DETALLE, Problem is in table T_DETALLE, only insert last data, I mean, table T_DETALLE have 2 rows, but first row is in blank, only second row have data.
    What is wrong ?. Pleas help me !!!

    Here, me code:

    Dim repositorio As RfcRepository = ConxSap.Repository
    Dim fZFMFI_2254 As IRfcFunction = repositorio.CreateFunction(“ZFMFI_2254″)

    Dim THeader = fZFMFI_2254.GetTable(“T_HEADER”)
    Dim TDetalle = fZFMFI_2254.GetTable(“T_DETALLE”)
    ‘—-header
    THeader.Insert()
    THeader.SetValue(“POSDO”, “1″)
    THeader.SetValue(“XBLNR”, pNroLiq)
    THeader.SetValue(“BKTXT”, “COMPRA DE CACAO”)
    ‘—-detail 1
    TDetalle.Insert()
    TDetalle.SetValue(“POSDO”, “1″)
    TDetalle.SetValue(“SAKNR”, cId_ctapuente1)
    TDetalle.SetValue(“WRBTR”, nTotal_bruto)
    ‘—-detail 2
    TDetalle.Insert()
    TDetalle.SetValue(“POSDO”, “1″)
    TDetalle.SetValue(“SAKNR”, cId_ctapuente2)
    TDetalle.SetValue(“WRBTR”, nTotal_fedeca)
    fZFMFI_2254.Invoke(ConxSap)

  15. Avatar
    Diego Dominguez
    March 4, 2016 at 3:56 pm · Reply

    The source code link is not valid

  16. […] Build an RFC Client with NCo 3.0 for … – Hi Craig, Thank you for the perfect explenation in details. It’s working fine for our project with VS 2010. Now we need to convert the solution in to the VS 2008 … […]

  17. Avatar
    may phat dien
    December 29, 2016 at 10:23 pm · Reply

    Thanks for your post! I hope you will continue to make similar posts like this! It is really necessary for those who want to learn about them!

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.