Skip to Content

Connecting to SAP with NCo 3

The SAP .NET Connector version 3 (NCo 3) is a vast improvement over previous versions. Within a couple hours of downloading it, I had whipped together a nifty, little prototype client application consuming BAPIs from several different application areas.

In the next few blog posts, I will discuss some of the tricks I discovered while messing around with this powerful tool. If you are just getting started with this tool, I hope these posts will help you over some features that weren’t so well documented. One of the major improvements in version 3 simplified the act of connecting to the R/3 back end, and that will be the subject of this post.


First off, some advice. SAP was kind enough to make a tutorial application available for download. Download it! Try to run it. Debug it. Study it closely. It gives working code examples of most of the major features of NCo 3. I found it a big help when the documentation and programming guide didn’t exactly make sense. There’s nothing like studying code examples. I will refer to the tutorial application from time to time in these posts.


The simplest, if least desirable, method of configuring your RfcDestination object is to hard code your logon parameters using the RfcConfigParameters object. Thomas Weiss provides a good example of this in his excellent blog. You can also put these parameters in the app.config file. This is well demonstrated in the tutorial application. A method I figured out through trial-and-error, since the documentation on this didn’t make much sense to me, was to leverage your SAP GUI’s saplogon.ini file. This, of course, assumes your client application is going to be running on a machine with the SAP GUI installed.


First, you must create an instance of the SapLogonIniConfiguration object and register it as your destination configuration. As if by magic, this is all it takes for the tool to find your saplogon.ini file. These two simple lines of code took me quite a while to figure out.

Dim mySapLogon As SapLogonIniConfiguration = SapLogonIniConfiguration.Create


Once you do this, you can get the logon parameters for the SAP instance you want to use by specifying its name. In this case, I want to log into our sandbox instance.

Dim myparm As RfcConfigParameters = mySapLogon.GetParameters(“ECCSBX”)

If you run your code, and stop it just after this line, you’ll see that myparm is filled with parameter data. All that came from your saplogon.ini. The next thing you need to do is set your desired SAP instance as an RfcDestination, and then from that, you must create a custom destination. I’m not sure why you can’t do this in one step, but you can’t.

Dim newDest As RfcDestination = RfcDestinationManager.GetDestination(“ECCSBX”)

Dim newCustomDest As RfcCustomDestination = newDest.CreateCustomDestination

Finally, you must pass in the missing parameters, such as user name, password, and client, to the custom destination. You could get this from the user in a dialog box, pull it from a database, from the app.config file, or hard code it as in my example below. It’s up to you.

newCustomDest.User = “batman”           ‘ don’t hard code this stuff

newCustomDest.Password = “jokersux”     ‘ … seriously, don’t

newCustomDest.Client = “110”            ‘ client could be a selection the user makes

Now, your destination object is fully configured from saplogon.ini information in eight, easy lines of code. Now, you are ready to take this destination object, start grabbing BAPIs from the repository, and do amazing things with SAP data. I will discuss how I did that in my next post.

You must be Logged on to comment or reply to a post.
  • Hi,

    I am trying to integrate SAP with .Net application using SAP .net connector. I want to create a Sales order in SAP. I have successfully connected to SAP but when I try to execute the BAPI “BAPI_SALESORDER_CREATEFROMDAT1” I get the following error:


    Following is the code snippet I am using :

    public string  CreateOrder(RfcDestination destination)




                     RfcRepository repo = destination.Repository;

                     IRfcFunction salesDoc = repo.CreateFunction(“BAPI_SALESORDER_CREATEFROMDAT1”);

                     IRfcFunction salesDocCommit = repo.CreateFunction(“BAPI_TRANSACTION_COMMIT”);

                     IRfcStructure salesHeader = salesDoc.GetStructure(“ORDER_HEADER_IN”);

                     IRfcTable salesItems = salesDoc.GetTable(“ORDER_ITEMS_IN”);

                     IRfcTable salesPartners = salesDoc.GetTable(“ORDER_PARTNERS”);

                     IRfcStructure soldtoparty = salesDoc.GetStructure(“SOLD_TO_PARTY”);


                     IRfcStructure salesItemsStruct = salesItems.Metadata.LineType.CreateStructure();

                     IRfcStructure salesPartnersStruct = salesPartners.Metadata.LineType.CreateStructure();

                     IRfcStructure salesPartnersStructRow2 = salesPartners.Metadata.LineType.CreateStructure();

                     //Sales Header

                     salesHeader.SetValue(“DOC_TYPE”, “ZDLR”);

                     salesHeader.SetValue(“SALES_ORG”, “1000”);

                     salesHeader.SetValue(“DISTR_CHAN”, “00”);

                     salesHeader.SetValue(“DIVISION”, “00”);

                     salesHeader.SetValue(“SALES_OFF”, “1001”);

                     //Sales Items

                     salesItemsStruct.SetValue(“ITM_NUMBER”, “323”);

                     salesItemsStruct.SetValue(“PLANT”, “1001”);

                     salesItemsStruct.SetValue(“MATERIAL”, “20000206”);

                     salesItemsStruct.SetValue(“TARGET_QTY”, “200”);

                     salesItemsStruct.SetValue(“REQ_QTY”, “200”);


                     // Partner

                     salesPartnersStruct.SetValue(“PARTN_ROLE”, “SP”);

                     salesPartnersStruct.SetValue(“PARTN_NUMB”, “102003”);





                     return “”;


                 catch (RfcCommunicationException e)


                     return e.ToString();



                 catch (RfcLogonException e)


                     // user could not logon…

                     return e.ToString();


                 catch (RfcAbapRuntimeException e)


                     // serious problem on ABAP system side…

                     return e.ToString();


                 catch (RfcAbapBaseException e)


                     return e.ToString();

                     // The function module returned an ABAP exception, an ABAP message

                     // or an ABAP class-based exception…



    Please help me resolve this, is there any thing I am missing or anything wrong with my call to BAPI. Any suggestions or solutions will be highly appreciated.


  • Dear Ed Hammerbeck ,

    I have a problem/exception in my case as explained as follows. I can able to connect to sap server from .net through SAP.netcon 3.0.

    My case:

    1. First I tried to connect SAP server using a username let it be SAPUSR01. It works fine and smooth.

    2. Later I tried to connect SAP server using another username let it be SAPUSR02. But it through an exception.

    How can I connect to SAP server with multiple username simultaneously? Please help me and let me resolve this.

    Thanks in advance.


    • Hi Janaraja,

      this depends on your use case:

      • In case you want to connect to different systems, you need to configure several destinations.
      • In case you want to connect to the same system with different users, create an RfcCustomDestination from the RfcDestination and set the user/password in this custom destination according to your needs. Please note that the user/password combination certainly has to fit …

      Best regards,


      • Hi Markus,

        I’m sorry for responding to such an old thread. I’m hoping you can help me.

        I’m currently working on a project where i have multiple different users connecting to SAP using the .net connector 3 from a mobile device. I was not getting any inconsistencies at first with less users but with more usage i’m receiving inconsistencies where the connection returned from the DestinationManager is not the connection expected. e.g destination name is different to the Destination user.

        Would creating an RfcCustomDestination from the original RfcDestination resolve this problem? I can only set the user and password properties but i cant change the RfcDestination name or should i not be worried about the name and just set the user and password?

        Your response is greatly appreciated.

        Thank you

        Lesego Thulo

        • Hi Lesogo,

          If I understood you correctly, you used destinations per user even though the configuration was identical if you exclude the user and password. If this is the case then using a RfcCustomDestination definitely makes sense as this is one of the targeted use cases.

          The destination name is only an identifier for the destination – which user is associated with it is not encoded in the name. Several custom destinations could have the same name, but could still have different users. Custom destinations are not really centrally managed, but only the pools associated with them.

          Best regards,


          • Thank you, Markus.

            I think that has solved my problem, when using RfcDestinationManager.GetDestination(“C921”) i would sometimes get a destination back that has a different user to the destination name. With result from GetDestination the user property is readOnly and by using CreatingCustom destination i’m able to make sure i’m using the correct destination not just because of the name but also because of the user.

            i have another question, when calling createCustomDestination, does this actually create a new destination? I have roughly 500 users that process transactions about 4 or 5 per minute. of the 500 i could say share about 90 connection/users. wouldnt this creation of customDestination overload my server with destination or connections to SAP?

            Thank you for you help

  • Hi,

    I have an old C# application which sends and receives IDOCS using version 2 of the .Net connector, but I now need to re-write this application to use version 3 of the SAP .Net connector.

    Do you know of any sample code which will show me how to do this in C#, as the version 3 library is completely different to version 2 and does not appear to have anything specific for sending and receiving IDOCs.



    • Knut, not really. I have not had a project to work on lately. I’ve just worked on the tutorials available here at SDN and elsewhere. We have a project ramping up soon (an open enrollment project for our benefits department) that will use NCo. Once we get into the development phase, I’ll take good notes and write an update here with some actual code.

  • Hi Ed,

    I have just started with SAP, RFCs, .Net and NCo, so I have a lot of catching up to do. I’ve written a working program for my company for which I now want to implement your idea but I’m stuck at the following part:

         Dim myparm As RfcConfigParameters =      mySapLogon.GetParameters(“ECCSBX”)

    I’m not quite sure what this “SAP instance” is, I need to enter as a parameter here. I’ve tried message servers and system IDs and whatnot, but whatever I enter, myparm stays empty and the RFC Destination “does not exist”. I’m sure it is just a rookie mistake, but I just can’t figure it out.

    • Tom, I haven’t looked at this stuff in a while, but I think this line makes reference to the SAP Logon pad on the client machine. “ECCSBX” is the name of our sandbox instance. If you don’t have SAP GUI installed with a Logon Pad, you’ll have to figure out something else, I’m afraid.