Click here to Skip to main content
15,307,716 members
Articles / Web Development / ASP.NET
Article
Posted 30 Sep 2014

Stats

151K views
43 bookmarked

SAP .Net Connector 3.0 (NCo) Example

Rate me:
Please Sign up or sign in to vote.
4.58/5 (18 votes)
30 Sep 2014CPOL6 min read
This is a simple example to show how to connect to SAP using SAP .Net Connector 3.0

Introduction

In some applications you need to send the data to SAP System and also get the data from them to display it on web pages. This article will help you to achieve that.

Background

This article defines how to connect to SAP RFC using SAP.Net Connector 3.0 called as NCo. This article expects that you have basic knowledge of development using C# and ASP.Net

Using the code

The most important part of SAP Connector  is to have required dlls:

  1. .Net Connector 3.0 DLL’s. There are 32 bit (x86) and 64 bit versions available for it

                       -sapnco.dll

                      -sapnco_utils.dll

To get the SAP Connector DLL, you need to go to SAP Market Place and you need to have userid and password to download it. If you are a .net developer, probably you do not have it. Ask the SAP Basis team to download and get it for you as they normally do have market place access.

If you get it from somewhere you are ready to go ahead.

If you are in development mode, using VS 2012/2013, you need to select Build platform Target as Any CPU. And add reference of 32-bit DLL even if you are using 64 bit machine.

On deployment to IIS7.5 or 8, if its 64 bit machine. Replace the 32-bit dll with 64-bit in the BIN Folder.

  1. SAP Destination Server configuration that needs to added to your config file.These are the main key-value pairs required to connect to SAP.

Image 1   

Launch the Visual Studio 2012 or 2013

  •  Create a web application SAPConnector3Example and add reference of sapnco.dll and sapnco_utils.dll
  •  Add a new class called SAPDestinationConfig that you will use to read the configuration
  •  Add SAP.Middleware.Connector namespace to this class which comes from the dll’s that you just added as reference in your project.

Image 2

  • To get the destination needed, SAP Connector provides an interface called IDestinationConfiguration. This interface provides a method called GetParameters.  You can use configuration manager to read this and implement this method.

Inherit IDestinationConfiguration to your class and right click on it to implement interface. After this step the class will appear as below

 

Image 3

  • GetParameters method returns RfcConfigParameters  which is class in SAP Connector DLL. Update the code to read the config values, and the code appears like this

 

  • Image 4
  •  Now the application is able to get the configuration, next step is to call this method. It can be called in a class where you would connect to the SAP. But the SAP connector has a method called RegisterDestinationConfiguration. All the destinations that have been defined like QA, DEV, PROD needs to be registered first before the call.

                   You can do this step just before connecting to the SAP when you instantiate the class. But think if                     many users are connected, each one would be calling RegisterDestinationConfiguration every time they                 make a call to SAP. So the best place to do it is Global.asax under Application_Start.

            As soon as you deploy your application and start it, there would be a destination connection already           registered and ready to be used.

            This will also resolve one of the important error messages, something like “destination configuration     already initialized”

 

  • Your Global.asax should look something like this. Make sure to include SAP.Middleware.Connector  namespace in this class as well

Image 5

  • The next step is to create a class which will act as a middleman between the .Net application and the SAP. This class will be responsible for connecting to SAP Server using SAP Connector, sending data and fetching data. Add a new class called SAPConnectorInterface. Add using SAP.Middleware.Connector namespace ,as this is also going to use the SAP Connector DLL.

Image 6

  • Let’s create a Test Connection to check if you are able to connect to SAP. RfcDestination class has a method Ping() which successfully pings, if the connection is established between your application and the SAP. If it’s not able to establish the connection, it will throw an exception.

Image 7

  • Now is the time to call SAP RFC to fetch data of the customers. Before stepping into this process, connect to you SAP Team Members and get the RFC’s that are available and written by them. Also check if they return the data back. RFC returns the data in parameters only, just like when you use ‘out’ keyword in a method call. Input Params in SAP is known as IMPORT and the values are returned as EXPORT params.
  • The RFC destination has one Repository. This repository contains complex RFC Functions and RFC structures. Sometimes you will find that, on SAP Side, they have a complex IRfcStructure which has IRfcFunction and viceversa. There functions returns a complex table structure called IRfcTable that you need to convert to ADO.Net Table.  Unfortunately SAP.Net connector does not have any default method that can be used. Use the following method to convert the IRfcTable to DataTable

Image 8

  • Create a method called GetCustomers which has two input parameters, country and destination name.

Create an instance of RFCRepository which has all the functions into it.

              rfcRepository = rfcDestination.Repository;

Using this repository call the method CreateFunction and provide the RFC name of SAP that you need to call. You need to make sure that the exact function name is available in SAP.

rfcFunction = rfcRepository.CreateFunction("RFC_CUSTOMER_DATA");

The input parameters to the RFC Functions are passed like this. It’s kind of Key Value Pair.  COUNTRY should be available as a parameter in the RFC in SAP Side.

rfcFunction.SetValue("COUNTRY", country);

 The function is called using this line

rfcFunction.Invoke(rfcDestination);

You will notice here that rfcDestination is being called, which contains all the needed information that is required by the SAP Connector to call SAP RFC.Image 9

Sometimes you will find that the RFC is returning a structure which has many RFC functions and each has different RFC tables. This needs to extracted like this

Image 10

This is a very simple example. You can send number of parameters to a RFC using rfcFunction.SetValue method. RfcFunction.Invoke(rfcDestination) is the line where the .Net application actually calls the SAP Function.

You will notice here, if you place a debug mark, that if you are sending big data to SAP and if SAP function is complex one and do many things at its end, the debug will get paused for some time and then it will move to the next debug line. This line is actually waiting for the call to come back to your application.

RegisterDestinationManager classs has one more method RfcDestinationManager.UnregisterDestinationConfiguration(destinationConfig), but you rarely would need to use it. 

Points of Interest

 

History

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

anand P Singh
Software Developer
United States United States
I am .Net developer working for a consulting firm.
I specialize in Microsoft.net technologies.

Comments and Discussions

 
QuestionSAP connector GetParametes method Pin
Member 979240618-Sep-19 21:05
MemberMember 979240618-Sep-19 21:05 
QuestionSample Pin
Regilmar13-Jun-19 4:05
MemberRegilmar13-Jun-19 4:05 
GeneralMy vote of 4 Pin
Fakher Halim6-Mar-19 0:47
MemberFakher Halim6-Mar-19 0:47 
AnswerGATEWAY_HOST missing Pin
aakashmca9-Jun-17 1:45
Memberaakashmca9-Jun-17 1:45 
QuestionSAP JCO .NET connector issue Pin
Dayal Mukati3-May-17 1:26
MemberDayal Mukati3-May-17 1:26 
GeneralOFFTOPIC Pin
Member 1263397713-Jul-16 14:52
MemberMember 1263397713-Jul-16 14:52 
Questionwhat i have to pass in Testconnection("?") in run time Pin
Salman_Faris13-Oct-15 2:07
MemberSalman_Faris13-Oct-15 2:07 
QuestionExcellent tutorial, Have you written unit test cases for this Pin
Member 1135455828-Jun-15 23:32
MemberMember 1135455828-Jun-15 23:32 
QuestionConfiguration on SAP side Pin
condee781-Mar-15 20:25
Membercondee781-Mar-15 20:25 
GeneralMy vote of 5 Pin
Ajitabh mohanty 201219-Dec-14 22:00
professionalAjitabh mohanty 201219-Dec-14 22:00 
QuestionI Want to implement this for Single sign on Pin
Member 1116924411-Dec-14 0:36
MemberMember 1116924411-Dec-14 0:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.