Click here to Skip to main content
15,861,168 members
Articles / Productivity Apps and Services / SAP

Connection to SAP from Visual Studio 2008 with SAP.NET Connector 2.0

Rate me:
Please Sign up or sign in to vote.
4.44/5 (15 votes)
24 Apr 2009CPOL3 min read 180.4K   48   34
This article explains how to connect to SAP from Visual Studio 2008 with SAP.NET Connector 2.0

Introduction

SAP is an inalienable system for large enterprise firms. We have to connect to this system if we need some data that belongs to SAP. The platforms can be Visual Studio 2008, Visual Studio 2005, Eclipse, or any other development tool.

But, there is a problem when connecting to SAP with Visual Studio 2008. The problem is SAP does not have a connector for Visual Studio 2005 and Visual Studio 2008. Up to Visual Studio .NET 2003 platform, there were no problems about connections to SAP.

As I mentioned in my other articles, the reason there is no connector for Visual Studio 2005 and Visual Studio 2008 is primarily marketing strategies. Let's not think why SAP did not make available an add-on for Visual Studio 2008, but think how can we solve this problem with available technologies.

Prerequisite Technologies

  • Visual Studio .NET 2003
  • Visual Studio 2008
  • SAP.NET Connector 2.0
  • Any SAP Application Server

Before reading this article, I suggest reading this article.

Using the Code

As I mentioned above, we will first use Visual Studio .NET 2003. Our purpose is to connect to SAP with Visual Studio 2003. While operating, we will create a class library project. Then, we will give a reference to this application to Visual Studio 2008. Thus, the problem about connection to SAP with Visual Studio 2008 will be solved. Meanwhile, this solution is recommended by SAP.

Let's start creating a class library project first.

sap_conn_vis2008_1.jpg

After creating the class library project, we will need to create a SAP Connector Proxy item as shown below.

sap_conn_vis2008_2.jpg

Because we will make a connection to SAP, we have to look at the SAP Servers under the Server Explorer section. We will use one of the BAPIs of SAP’s user information. This is the general view of the BOR objects:.

sap_conn_vis2008_3.jpg

We will use the ‘GetDetail’ method of the USER object. Drag and drop this method onto the SAPProxy1.sapwsdl file. Later on, there is no need for another class in our project. I am deleting the Class1.cs file.

sap_conn_vis2008_5.jpg

We have now completed the operations in the Visual Studio .NET 2003 side. You should now build your project. The output ‘SAPUserDetailComponent.dll’ will be a required file while connecting to SAP in Visual Studio 2008.

Now, we will create a Windows project under Visual Studio 2008.

sap_conn_vis2008_6.jpg

Before adding the DLL file of the previous project, we should add two more DLLs to our project. First, copy ‘SAPUserDetailComponent.dll’ to your project folder. Then, go to “..\Program Files\SAP\SAP .NET Connector 2.0″ and copy “SAP.Connector.dll” and “SAP.Connector.Rfc.dll” to your project folder.

sap_conn_vis2008_7.jpg

sap_conn_vis2008_9.jpg

We should pay attention to another point. The SAP Proxy is based on the SOAP protocol. So, we need to add the “System.Web.Services” DLL to our references.

sap_conn_vis2008_10.jpg

I have only added simple controls into Form1. The logic is simple, a SAP username will be taken, and the profiles authorization of this user will be printed into a table to show. Form1 will look as shown below.

sap_conn_vis2008_11.jpg

First, I inform the Visual Studio builder about the usage of the SAPUserDetailComponent assembly file:

C#
using SAPUserDetailComponent;

Then, I write the code for the GetProfiles button’s Click event.

C#
SAPProxy1 proxy = new SAPProxy1();

The SAPProxy1 class is referenced in our SAPUserDetail component reference.

C#
BAPIADDR3 address = new BAPIADDR3();
BAPIUSCOMP companyName = new BAPIUSCOMP();
BAPIDEFAUL defaults = new BAPIDEFAUL();
BAPILOGOND logonData = new BAPILOGOND();
BAPISNCU snc = new BAPISNCU();
BAPIAGRTable activeGroups = new BAPIAGRTable();
BAPICOMREMTable addcomrem = new BAPICOMREMTable();
BAPIADFAXTable addfax = new BAPIADFAXTable();
BAPIADPAGTable addpag = new BAPIADPAGTable();
BAPIADPRTTable addprt = new BAPIADPRTTable();
BAPIADRFCTable addrfc = new BAPIADRFCTable();
BAPIADRMLTable addrml = new BAPIADRMLTable();
BAPIADSMTPTable addsmtp = new BAPIADSMTPTable();
BAPIADSSFTable addssf = new BAPIADSSFTable();
BAPIADTELTable addtel = new BAPIADTELTable();
BAPIADTLXTable addtlx = new BAPIADTLXTable();
BAPIADTTXTable addttx = new BAPIADTTXTable();
BAPIADURITable adduri = new BAPIADURITable();
BAPIADX400Table addx400 = new BAPIADX400Table();
BAPIPARAMTable parameter = new BAPIPARAMTable();
BAPIPROFTable profiles = new BAPIPROFTable();
BAPIRET2Table return0 = new BAPIRET2Table();

The above variables will be used while calling the SAP function.

C#
SAP.Connector.Destination destination = new SAP.Connector.Destination();

We need to create a destination object to connect SAP. Later on, we will set the properties of this destination object as I have shown below.

C#
destination.AppServerHost = YOUR HOST NAME OR IP
destination.Client = YOUR CLIENT NUMBER
destination.Password = USER'S PASSWORD
destination.SystemNumber = SYSTEM NUMBER
destination.Username = USER'S NAME

Our destination object will be a parameter for the SAPConnection class.

C#
SAP.Connector.SAPConnection connection = 
   new SAP.Connector.SAPConnection(destination);

And, our connection object will be assigned to the proxy object.

C#
proxy.Connection = connection;

Now, we open the connection.

C#
proxy.Connection.Open();

Call the SAP function:

C#
proxy.Bapi_User_Get_Detail(tbUsername.Text.Trim(), out address, out companyName, 
   out defaults, out logonData, out snc, ref activeGroups, 
   ref addcomrem, ref addfax, ref addpag, ref addprt, ref addrfc, 
   ref addrml, ref addsmtp, ref addssf, ref addtel, ref addtlx, 
   ref addttx, ref adduri, ref addx400, 
   ref parameter, ref profiles, ref return0);

Then, close the connection.

C#
proxy.Connection.Close();

And, I will convert the result table of this function to an ADO table as I have shown below:

C#
DataTable dt = profiles.ToADODataTable();

The last operation is we give the data table as a source for the grid view.

C#
dgProfiles.DataSource = dt; 

The result screen will be:

sap_conn_vis2008_12.jpg

You can also visit my technical blog site for my other articles.

License

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


Written By
Software Developer (Senior)
Turkey Turkey
Huseyin Akturk was born in Ankara, Turkey. He holds BSc degree from Dokuz Eylul University, Izmir, Turkey Computer Engineering department since July 2005. He also holds MSc degree from Ege University, Izmir, Turkey Computer Engineering department since March 2008.

He has applied data mining technology and statistical operations on stock exchanges during BSc and MSc theses. The purpose of these theses is looking for similarity between stock exchanges and he has got successful results in these theses respect to the purpose and published essays.

He has worked with a responsibility of being computer engineer during professional and academic experience. He has assimilated the idea of being keen on newness in technology. He has been working as Senior SAP Business Intelligence Consultant, Senior SAP Technical Consultant and Senior Software Engineer for several firms since October 2005.

Comments and Discussions

 
QuestionI receive this error message after I login to sap i cant read the tables info from sapmy error message is as follows Pin
Member 1211862015-Nov-15 4:23
Member 1211862015-Nov-15 4:23 
QuestionAccess a SAP Program in dot net windows Application Pin
Raju from lucknow12-Mar-14 21:06
Raju from lucknow12-Mar-14 21:06 
QuestionAn alternative .NET container from VS Studio Pin
MYLES MORRIS29-Nov-11 13:40
MYLES MORRIS29-Nov-11 13:40 
GeneralSend data to SAP Ztable Pin
Member 231241710-Mar-11 19:15
Member 231241710-Mar-11 19:15 
GeneralMy vote of 4 Pin
Member 231241710-Mar-11 19:11
Member 231241710-Mar-11 19:11 
GeneralMy vote of 4 Pin
Rajeev Kumar2228-Dec-10 0:10
Rajeev Kumar2228-Dec-10 0:10 
GeneralMy vote of 3 Pin
pealiroy2005@gmail.com19-Oct-10 2:42
pealiroy2005@gmail.com19-Oct-10 2:42 
GeneralRe: My vote of 3 Pin
huseyinakturk19-Nov-10 1:40
huseyinakturk19-Nov-10 1:40 
GeneralError on parameters used in Bapi_User_Get_Detail Pin
OmarGP22-Sep-10 8:29
OmarGP22-Sep-10 8:29 
GeneralPaste this with all parameters -> Error on parameters used in Bapi_User_Get_Detail Pin
Axel Arnold Bangert27-Sep-10 4:46
Axel Arnold Bangert27-Sep-10 4:46 
GeneralRe: Paste this with all parameters -> Error on parameters used in Bapi_User_Get_Detail Pin
OmarGP27-Sep-10 8:21
OmarGP27-Sep-10 8:21 
GeneralRe: Paste this with all parameters -> Error on parameters used in Bapi_User_Get_Detail Pin
Ashishgadekar23-Jan-13 17:58
Ashishgadekar23-Jan-13 17:58 
Questiondrag and drop ok? Pin
Odella Chen1-Jul-10 20:35
Odella Chen1-Jul-10 20:35 
AnswerRe: drag and drop ok? Pin
huseyinakturk21-Jul-10 6:02
huseyinakturk21-Jul-10 6:02 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer14-Apr-10 3:10
Izzet Kerem Kusmezer14-Apr-10 3:10 
GeneralFrom .net app to sap Pin
saperman9-Feb-10 22:24
saperman9-Feb-10 22:24 
GeneralRe: From .net app to sap Pin
huseyinakturk9-Feb-10 22:28
huseyinakturk9-Feb-10 22:28 
GeneralVery helpful Pin
$2008-Dec-09 14:57
$2008-Dec-09 14:57 
GeneralRe: Very helpful Pin
huseyinakturk13-Dec-09 5:15
huseyinakturk13-Dec-09 5:15 
GeneralRe: Very helpful Pin
$2006-Jan-10 17:25
$2006-Jan-10 17:25 
GeneralRe: Very helpful Pin
peterklausen21-Feb-10 14:24
peterklausen21-Feb-10 14:24 
GeneralThank you! Pin
mthd733-Aug-09 11:57
mthd733-Aug-09 11:57 
GeneralRe: Thank you! Pin
huseyinakturk4-Aug-09 5:06
huseyinakturk4-Aug-09 5:06 
QuestionVery useful and informative. Thanks. I need help Pin
Viji Raj25-Jun-09 14:37
Viji Raj25-Jun-09 14:37 
AnswerRe: Very useful and informative. Thanks. I need help Pin
huseyinakturk27-Jun-09 8:26
huseyinakturk27-Jun-09 8:26 

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.