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.
- 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)
- 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)
- 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.
- 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)
- 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)
- 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)
- 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)
- 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)
- 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.
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
Avendra, I will try to create a web project example and post it in the next couple of weeks. Please check back.
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,
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
problem in NCO 3.0.3.0 convert time
Details
http://forums.sdn.sap.com/message.jspa?messageID=10830236#10830236
How can pass Table parameters with NCo 3.0 ?We have many such BAPI’s.
E se eu quiser altera o “PASSWD” por exemplo??
A senha é armazenada no arquivo de configuração. Mudá-lo à vontade.
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.
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
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
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.
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
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.
Hello,
I’ve tried your option, but I’m receiving the same error.
You can look at this link:
Regards,
Hi
Can you somebody help me, how can I update Vendors data in SAP using RFC?
Thank you
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
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”
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
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
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:DeveloperBhaveshBackupBackup20160222Sap_IntigrationSap_IntigrationSAPRadForm1.vb:line 28
InnerException:
HI , Thanks for the information its very helpful,
But could not download the Source file.please help us to download the Source file
getting Error on Company, not defined…
regards
Xaim Raza
it is very helpful. Anyone pl share the source code to download
Saved my day! Many thanks!!!