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.