Asp.net Connect with SAP RFC
In this article I’ll discuss how to set up a connection between an SAP and the .Net Framework using C#. this article will focus all information related to connect with SAP.
Prerequisites
Microsoft Visual Studio 2010 or higher (I’am using Visual Studio 2015).
Install SAP GUI Software
Go to Below Link for downloading SAP GUI Software
Steps to Integrate Asp.net with SAP RFC
First Install SAP GUI Software on your system.
After installing restart your system.
Required .dll and .ocx File to get from below path
c:\program files (x86)\sap\frontend\sapgui\wdobapi.ocx
c:\program files (x86)\common files\sap shared\wdtlog.ocx
c:\windows\SysWOW64\librfc32.dll
c:\program files (x86)\sap\frontend\sapgui\wdtaocx.ocx
c:\program files (x86)\sap\frontend\sapgui\wdtfuncs.ocx
Now Create Project using VS 2015
Open Visual Studio 2015 ->File ->New -> Project -> Visual C# -> Web ->Asp.net Web Application.
Name section Specify Project name and select Location where you want save your project as below screen and click on OK button.
Screen will be opened then select Empty and check Web Form and Click On Ok button then your project will be created.
Required to Add below References -
Wdobapi.ocx
WDTAOCX.ocx
Wdtfuncs.ocx
Wdtlog.ocx
Go to Solution Explorer -> Right Click Reference -> Add reference -> then window will open their click on Browse button then go to above specified path and add .dll one by one.
Now Start Coding using c#
Add references
using System.Data;
using SAPFunctionsOCX;
protected void Page_Load(object sender, EventArgs e)
{
string SystemNumber = "01";
string ApplicationServer = "172.16.22.115"; //QAS
string Client = "500"; //Quality
string User = "rfcuser";
string Password = "12345678";
string Language = "EN";
string SAProuter = "/H/14.140.246.46/W/N@pin0@242/H/";
DataTable _header = new DataTable();
_header.Columns.Add("EMP_ID", typeof(string)); //Employee Code
_header.Columns.Add("EMP_NAME", typeof(string)); //Employee Name
_header.Columns.Add("EMP_EMAIL", typeof(string)); //Employee Email Id
_header.Columns.Add("EMP_MOB", typeof(string)); //Mobile No
_header.Columns.Add("EMP_PAN", typeof(string)); //PAN No
_header.Columns.Add("EMP_LOC", typeof(string)); //Location
_header.Columns.Add("EMP_DEPT", typeof(string)); //Department
_header.Columns.Add("EMP_DESIG", typeof(string)); //Designation
_header.Columns.Add("PLSTX", typeof(string)); //Designation Name
_header.Columns.Add("ORGTX", typeof(string)); //Department Name
_header.Columns.Add("PLANT_CODE", typeof(string)); //Plant Code
_header.Columns.Add("PLANT_NAME", typeof(string)); //Plant Name
_header.Columns.Add("USRID", typeof(string)); //Mobile No
_header.Columns.Add("GENDER", typeof(string)); //Gender
_header.Columns.Add("BUKRS", typeof(string)); //Company Code
_header.Columns.Add("BUTXT", typeof(string)); //Company Name
object oFunc;
SAPFunctionsOCX.SAPFunctions Sapobj = new SAPFunctionsOCX.SAPFunctions();
Sapobj.Connection.SystemNumber = SystemNumber;
Sapobj.Connection.ApplicationServer = ApplicationServer;
Sapobj.Connection.Client = Client;
Sapobj.Connection.User = User;
Sapobj.Connection.Password = Password;
Sapobj.Connection.Language = Language;
Sapobj.Connection.SAProuter = SAProuter;
if (Sapobj.Connection.Logon(1, true))
{
//Assign RFC
var ifunc = Sapobj.Add("ZHR_EMPLOYEE_DATA");
ifunc.Exports("P_PERNR").Value = "EMP001"; //take parameter
if (ifunc.Call())
{
//HEAER TABLES
SAPTableFactoryCtrl.Tables PO = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
SAPTableFactoryCtrl.Table PO_HEADER = (SAPTableFactoryCtrl.Table)PO.get_Item("EMPLOYEE_DETAIL");
DataRow dr;
if (PO_HEADER.Rows.Count > 0)
{
for (int p = 1; p <= PO_HEADER.Rows.Count; p++)
{
dr = _header.NewRow();
dr["EMP_ID"] = PO_HEADER.get_Cell(p, "EMP_ID");
dr["EMP_NAME"] = PO_HEADER.get_Cell(p, "EMP_NAME");
dr["EMP_EMAIL"] = PO_HEADER.get_Cell(p, "EMP_EMAIL");
dr["EMP_MOB"] = PO_HEADER.get_Cell(p, "EMP_MOB");
dr["EMP_PAN"] = PO_HEADER.get_Cell(p, "EMP_PAN");
dr["EMP_LOC"] = PO_HEADER.get_Cell(p, "EMP_LOC");
dr["EMP_DEPT"] = PO_HEADER.get_Cell(p, "EMP_DEPT");
dr["EMP_DESIG"] = PO_HEADER.get_Cell(p, "EMP_DESIG");
dr["PLSTX"] = PO_HEADER.get_Cell(p, "PLSTX");
dr["ORGTX"] = PO_HEADER.get_Cell(p, "ORGTX");
dr["PLANT_CODE"] = PO_HEADER.get_Cell(p, "PLANT_CODE");
dr["PLANT_NAME"] = PO_HEADER.get_Cell(p, "PLANT_NAME");
dr["USRID"] = PO_HEADER.get_Cell(p, "USRID");
dr["GENDER"] = PO_HEADER.get_Cell(p, "GENDER");
dr["BUKRS"] = PO_HEADER.get_Cell(p, "BUKRS");
dr["BUTXT"] = PO_HEADER.get_Cell(p, "BUTXT");
_header.Rows.Add(dr);
}
}
}
}
//Save Data in SAP
if (Sapobj.Connection.Logon(1, true))
{
IFunction ifunc = (IFunction)Sapobj.Add("ZHR_ITTAX_DATA");
IStructure TAX_INPU_DATA = (IStructure)ifunc.get_Exports("GW_TAX");
// var po_item = ifunc.Tables("T_ITEM_DATA");
TAX_INPU_DATA.set_Value("GJAHR", "");
TAX_INPU_DATA.set_Value("EMP_ID", "");
TAX_INPU_DATA.set_Value("EMP_NAME", "");
TAX_INPU_DATA.set_Value("EMP_EMAIL", "");
ifunc.Call();
var ReturnMsg = ifunc.get_Imports("G_SUCCESS").Value;
string Rval = ReturnMsg;
}
}
Execute your Code and if you will Get below Error
Could not load file or assembly 'sapnco_utils' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Go to references and Remove sapnco_utils. Your problems will be Solved