Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / Visual Basic
Article

Using the SAP.NET Connector to Connect your .NET Applications to SAP

Rate me:
Please Sign up or sign in to vote.
4.40/5 (22 votes)
6 Mar 2008CPOL6 min read 416.9K   17.2K   77   40
An article about using the SAP.NET connector to connect your .NET applications to SAP

Overview

This article will show how to establish a connection to SAP and how to call an ABAP function on SAP and pass data to the function and receive data back from the function all via the SAP .NET Connector.

Introduction

If you are a .NET Software Developer, it's almost a given that you have been tasked at one time or another to interact with various ERP systems. Sometimes, this task can be completed simply because you may be granted access directly to the back end databases, however often, you need to communicate directly with the business logic layer of the ERP system. This is often the case when working with SAP because of the powerful ABAP functions which reside within the business logic layer of the system. Fortunately, SAP and Microsoft have teamed up to provide developers with the SAP.NET Connector.

The SAP Connector for Microsoft .NET is a programming environment that enables communication between the Microsoft .NET platform and SAP Systems. It supports SAP Remote Function Calls (RFC) and Web services, and allows you to write various applications, for example, Web form, Windows form, and console applications within Microsoft Visual Studio .NET. You can use all Common Language Runtime (CLR) programming languages such as Visual Basic .NET and C#. This article will demonstrate how to get connected to SAP and how to call an ABAP function and pass data to and from SAP. So, without further ado, let's get to the code!

Establishing a Connection to SAP at Design Time

The first thing to note here is that your .NET applications can interact with SAP in two ways. First, as a client which calls SAP, the server. The second way in which your .NET applications can interact with SAP is where your client acts as the server and receives calls from SAP, the client. This article will focus on the former, and the latter will be discussed in a forthcoming article. It should be noted that connecting your .NET applications to SAP as the client is much more straightforward than when your .NET applications have to act as the server.
So, how do we connect a .NET application to SAP? Well, the first thing you need to do is download the SAP.NET connector, which is free and can be obtained here. Once you have installed the connector, start Visual Studio .NET and create a new Windows application. You might be tempted to go straight to the references of the project and add references to the SAP .NET connector, however, this approach is not correct. The correct next step is to right click on your project file and select Add, then New item. Once the dialog box opens, scroll to the bottom and you will find an entry called “SAP Connector Proxy”, as show in figure 1 below:

SAP Proxy Application Development
Figure 1 – Adding the SAP Connector Proxy to your project.

After you add the proxy, you will notice that the system automatically adds the necessary references to your project as well as creates a WSDL file called SAPProxy1.wsdl. This file will show up as a blank screen in Visual Studio, as shown in figure 2 below:

SAP Application Development
Figure 2: After the SAP Connector Proxy has been added.

Once the proxy has been added, it's time to add the SAP Server to your project. Click View, then click Server Explorer and you will see that an option for SAP is included in the list. Click the SAP entry and it will expand and show "Application Servers". Right click this and select "Add Application Server". A dialog box similar to Figure 3 below will appear:

SAP .NET Software Development
Figure 3 – SAP Application Server Dialog Box.

This is where you will need to set your parameters for establishing a connection to your SAP server. Change the Destination Type property to “Custom”, then add the necessary values for the following properties:

  • User Name
  • Password
  • AppServerHost
  • SAPRouterString
  • HTTP Port
  • Client
  • System Number

These variables will depend on your SAP installation. Check with your SAP Administrator for the specific settings in your environment.

Once you have the properties set, you can then establish your connection to SAP. Expand the new server, then expand the Functions entry. This will provide a list of all ABAP functions that are contained in your SAP environment. See figure 4 below:

ABAP Development Company

The next step is to locate the function that you wish to call. When you find it, simply drag and drop it onto the SAPProxy1.sapwsdl file. It will automatically create several objects and even code files that relate to your particular function (note that you need to be viewing all files in your project to see the code files that the process creates):

SAP .NET Connector
Figure 5: After the SAP objects are created,
note the additional code files automatically created.

Once this step is created, the rest is quite easy. To demonstrate, my sample uses a very simple ABAP function that takes a contract number and a line item number and returns an invoice. I have created a form with 1 button and a textbox. The code behind my button will do the following:

  • Establish a connection to SAP
  • Create an object to allow me to invoke the ABAP function and return the values that it provides.

Here is the code listing (note that I have removed the actual connection values – you need to substitute the values with your connection parameters):

VB.NET
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    Dim BAPI As New SAPProxy1
    Dim conStr As String = "ASHOST=AppserverIP SYSNR=sysnumber CLIENT=clientnumber _
                           USER=user PASSWD=password"
 


    BAPI = New SAPProxy1(conStr)

    Dim SAPtbl As New ZSDUS_INVOICETable
    Dim Ret As New VS2003_SAP_Connect.BAPIRETURNTable

    Try
        'params are contract # and line item
        BAPI.Zsdus_Get_Invoice_Number("0045001126", "000010", SAPtbl, Ret)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    'ret table of success or failure
    'ret table is always empty upon success if ret contains a
    'value that indicates failure
    If Ret.Count > 0
    Then
        MsgBox(Ret.Item(0).Message.ToString)
    End If

    TextBox1.Text = (SAPtbl.Item(0).Vbeln.ToString)
End Sub

So what’s going on here?
We do the following:

  • Declare a variable to hold the connection string.
  • Create an instance of our Proxy.
  • Establish the connection.
  • Create the parameters that the ABAP function calls for. In this case its two tables – note these are strongly typed.
  • Call the function passing in the parameters.
  • Examine the Return table to see if we had success or any errors.
  • Read out the first item from the table, a field called Vbeln and assign its value to a text box.

See the results below:

SAP ERP Programming
Figure 6 – The results.

As you can see, the function call returned a data table with invoice information. I have chosen to only display one field for simplicity sake, but the actual data returned is all the contents of the invoice. We could have loaded this information to a data grid, for example.

Conclusion

This article shows a real world example of connection to SAP and calling an ABAP function from .NET via the SAP.NET connector. As you can see, by using the connector as a client to SAP, we can easily connect to the SAP server and invoke any function that we have permissions to execute. This ability add tremendous flexibility to your .NET applications when you need to integrate with SAP.

About the Author

Andrew Schmidt is a consultant with Custom Software By Preston. He has over 10 years of software development experience in a range of industries including manufacturing, government, health care, and telecommunications. He has developed enterprise systems that integrate with SAP and other ERP systems. He resides in Chicago with his wife and 1 year old son and can be reached via email at andy@customsoftwarebypreston.com or at Custom Software by Preston.

License

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


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs this 3.0 or 4.0- Dot Net Connector? And No source code, only .DLL's Pin
Member 127281058-Sep-16 6:38
Member 127281058-Sep-16 6:38 
GeneralMy vote of 4 Pin
DuraiAmuthan25-Sep-13 4:37
DuraiAmuthan25-Sep-13 4:37 
Questioncoonector 3.0 Pin
cookie_monsta6-Mar-13 21:39
cookie_monsta6-Mar-13 21:39 
QuestionHas anyone tried the above example? Pin
Andreas Kasapleris6-Mar-13 12:16
Andreas Kasapleris6-Mar-13 12:16 
I want to ask if anyone has tried the above. Although I have installed SAP Connector with VS2010
nothing of the above example seems to work. SAP Proxy does not even appear as on option!
AnswerRe: Has anyone tried the above example? Pin
Jim F. Johnson14-Jan-15 18:34
Jim F. Johnson14-Jan-15 18:34 
QuestionConnecting to SAP from .net using single sign on Pin
kasun.uggalla21-Nov-11 23:26
kasun.uggalla21-Nov-11 23:26 
GeneralMy vote of 5 Pin
kasun.uggalla21-Nov-11 23:23
kasun.uggalla21-Nov-11 23:23 
QuestionSource Code missing !!!!! Pin
User 646553529-Nov-10 3:54
User 646553529-Nov-10 3:54 
GeneralError I::001 Only available with the RFC library from 4.0C onwa Pin
Alexey12018-Jan-10 1:07
Alexey12018-Jan-10 1:07 
GeneralDownload SAP Connector 2.0 Pin
bojanv553-Dec-09 6:33
bojanv553-Dec-09 6:33 
GeneralRe: Download SAP Connector 2.0 Pin
peterklausen21-Feb-10 14:23
peterklausen21-Feb-10 14:23 
QuestionWas there ever an article that showed how to make a server to listen for messages from SAP? (NM) Pin
eddiem92-Sep-09 6:04
eddiem92-Sep-09 6:04 
AnswerRe: Was there ever an article that showed how to make a server to listen for messages from SAP? (NM) Pin
goeli2-Feb-12 1:37
goeli2-Feb-12 1:37 
GeneralUnable to Establish connection to R/3 System xxxx.xx.x.xxx System(Client=0,System=1) [modified] Pin
kyreddy25-Aug-09 18:08
kyreddy25-Aug-09 18:08 
GeneralRe: Unable to Establish connection to R/3 System xxxx.xx.x.xxx System(Client=0,System=1) [modified] Pin
Navin Pai2-Feb-12 1:12
Navin Pai2-Feb-12 1:12 
GeneralRe: Unable to Establish connection to R/3 System xxxx.xx.x.xxx System(Client=0,System=1) [modified] Pin
Navin Pai2-Feb-12 1:15
Navin Pai2-Feb-12 1:15 
QuestionCan't connect to Server SAP to get the functions Pin
FlorianBCN30-Oct-08 23:52
FlorianBCN30-Oct-08 23:52 
AnswerRe: Can't connect to Server SAP to get the functions Pin
Member 9482601-Nov-08 13:59
Member 9482601-Nov-08 13:59 
GeneralRe: Can't connect to Server SAP to get the functions Pin
Check here to remove your current profile picture1-Dec-08 22:09
Check here to remove your current profile picture1-Dec-08 22:09 
GeneralRe: Can't connect to Server SAP to get the functions Pin
juarez.ebpetroleo3-Mar-09 9:35
juarez.ebpetroleo3-Mar-09 9:35 
AnswerRe: Can't connect to Server SAP to get the functions Pin
Member 115685839-Apr-15 19:03
Member 115685839-Apr-15 19:03 
GeneralNo SAP server in server explorer Pin
boethius3-Oct-08 1:14
boethius3-Oct-08 1:14 
GeneralRe: No SAP server in server explorer Pin
Srinivas-Miriyala15-Oct-08 4:04
Srinivas-Miriyala15-Oct-08 4:04 
GeneralRe: No SAP server in server explorer Pin
Sunil_Pawar27-Nov-08 23:29
professionalSunil_Pawar27-Nov-08 23:29 
GeneralSAP.NET Connector for 64 Bit Pin
Member 37136235-Aug-08 14:34
Member 37136235-Aug-08 14:34 

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.