Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Link to Content's target Space : 

http://wiki.sdn.sap.com/wiki/pages/editpage.action?pageId=263291953   

Applies to: 

SAP .Net technologies. 

Summary

Here i am explaining how to read,update and insert data into sap database by using c#.net including login authentication and working with irftable,irfcstructure and function. 

Author(s):   

   
Company:     ERP info solutions pvt.ltd
Created on:    10/01/2012
Author(s) Bio Harish Yarra 

Step 1:  First we need to install SAP.Net Connector into machine. we don't have any SAP.Net connector please contact  sap  basis team. 

Step 2: Add sapnco.dll, sapnco_utils.dll into your solution. After installing the sap.Net Connector into your machine           

these .dll's are available in c->program files>SAP->SAP.Net connector folder.

 Step3: Open your .cs or .vb code behind file.

Note: Here I am taking example for C#.net. Lot of free C#.net to VB.Net connector’s available in market.

copy this below code into .cs/.vb file.

///Below code is useful to configure the server connection string from .net. Here i am taking sample connection parameters.///Below code is useful to configure the server connection string from .net. Here i am taking sample connection parameters.

 public class MyBackendConfig : IDestinationConfiguration
        {
            // RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());
            public RfcConfigParameters GetParameters(String destinationName)
            {
                if ("SE37".Equals(destinationName))
                {
                     RfcConfigParameters parms = new RfcConfigParameters();
                    parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.14");
                    parms.Add(RfcConfigParameters.SystemNumber, "02");
                    parms.Add(RfcConfigParameters.User, "abaper");
                    parms.Add(RfcConfigParameters.Password, "erp@1234");
                    parms.Add(RfcConfigParameters.Client, "800");
                    parms.Add(RfcConfigParameters.Language, "EN");
                    parms.Add(RfcConfigParameters.PoolSize, "5");
                    parms.Add(RfcConfigParameters.MaxPoolSize, "10");
                    parms.Add(RfcConfigParameters.IdleTimeout, "600");
                    return parms;
                }
                else return null;
            }
            public bool ChangeEventsSupported()
            {             
   return false;
            }
            public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
            // public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
        }
    }

// In page_load event add this below code for regitering the server settings.
Step4:  
 MyBackendConfig config__ = new MyBackendConfig();

                try
                {
               RfcDestinationManager.RegisterDestinationConfiguration(config__);
                 }
                catch (RfcInvalidStateException exec)
                {
                     Label1 .Text= exec.Message;
                 }

                catch (RfcCommunicationException exec2)
                {
                    Label1.Text = exec2.Message;
                }

Step5 :

How to Read the values from sap database and displayed into ASP.net Grid view control.

//here I am taking the example for displaying the employee details based upon the empid.

// Show button click copy this code.

//Note:  Ask BAPI names, IMOPRT parameters’ and EXPORT parameters from ABAPERS. Here I am taking sample bapi names and parametrs


                 RfcDestination prd = RfcDestinationManager.GetDestination("SE37");

                RfcRepository repo = prd.Repository;

              // BAPI_ADDREMPAU_GETDETAIL is bapi please take bapi name from abaper.

               IRfcFunction companyBapi1 = repo.CreateFunction("BAPI_ADDREMPAU_GETDETAIL");

             // set the employeenumber based upon the employeenumber we need to display empdetails.

               companyBapi1.SetValue("EMPLOYEENUMBER", int.Parse(txtEmploeeNumber.Text));

             // Invoke the rfcfunction

             companyBapi1.Invoke(prd);

                for (int i = 0; i < companyBapi1.ElementCount; i++)

                {

                    RfcDirection direction = companyBapi1[i].Metadata.Direction;

 if(companyBapi1[i].Metadata.DataType.ToString().Trim()!= "STRUCTURE" && direction.ToString().Trim()=="EXPORT")
                    {
                        dt.Columns.Add(companyBapi1.Metadata[i].Name);
                    }

                }
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                int columnnumber = 0;
                for (int m = 0; m < companyBapi1.Count; m++)
                {
                    RfcDirection direction = companyBapi1[m].Metadata.Direction;
                    if (companyBapi1[m].Metadata.DataType.ToString().Trim()!= "STRUCTURE" && direction.ToString().Trim() =="EXPORT" && columnnumber<dt.Columns.Count)
                    {
                        dt.Rows[0][columnnumber] = companyBapi1[m].GetString();
                    }
                    columnnumber++;
                }
            }
            catch (RfcInvalidParameterException exec)
             {
                Label1.Text = exec.Message;
             }
            catch (RfcTypeConversionException exec1)
            {
                Label1.Text = exec1.Message;
             }

            catch (RfcCommunicationException exec2)

            {

              Label1.Text = exec2.Message;

            }

            catch (RfcBaseException exec3)

            {

            Label1.Text = exec3.Message;

            }

            GridView1.DataSource = dt;


             GridView1.DataBind(); 
Step 6: How to insert values into sap database from .net

              ///here i am taking example for inserting values into database.

              RfcDestination prd = RfcDestinationManager.GetDestination("SE37");

                RfcRepository repo = prd.Repository;

              // BAPI_ADDREMPAU_InsertDETAIL is bapi please take bapi name from abaper.       

               IRfcFunction companyBapi1 = repo.CreateFunction("BAPI_ADDREMPAU_InsertDETAIL");     

             // set the employee first name,lasename and DOB for inserting data those details into database.


                companyBapi1.SetValue("FirstName", txtFirstName.Text);

               companyBapi1.SetValue("LastName", txtLastName.Text);

               companyBapi1.SetValue("DOB", txtDOB.Text);

               companyBapi1.Invoke(prd);