7 Replies Latest reply: Apr 12, 2013 6:04 PM by Kiran Pedda RSS

Call BAPI using SAP .NET Connector 3.0

Shafi AM
Currently Being Moderated

Dear

 

I installed visual studio 2008 and SAP .NET connector 3.0.

 

How can I call a BAPI from vb.net ?

  • Re: Call BAPI using SAP .NET Connector 3.0
    Case Ahr
    Currently Being Moderated

    First, create a class that implements IDestinationConfiguration.  VB.NET code shown below.

     

    Imports SAP.Middleware.Connector
    
    Public Class ECCDestinationConfig
        Implements IDestinationConfiguration
    
        Public Event ConfigurationChanged(ByVal destinationName As String, ByVal args As RfcConfigurationEventArgs) Implements IDestinationConfiguration.ConfigurationChanged
    
        Public Function GetParameters(ByVal destinationName As String) As RfcConfigParameters Implements IDestinationConfiguration.GetParameters
    
            Dim parms As New RfcConfigParameters
    
            Select Case destinationName
    
                Case "ECDCLNT140"
    
                    parms.Add(RfcConfigParameters.AppServerHost, "10.1.1.1")
                    parms.Add(RfcConfigParameters.SystemNumber, "00")
                    parms.Add(RfcConfigParameters.SystemID, "ECD")
                    parms.Add(RfcConfigParameters.User, "username")
                    parms.Add(RfcConfigParameters.Password, "secret")
                    parms.Add(RfcConfigParameters.Client, "140")
                    parms.Add(RfcConfigParameters.Language, "EN")
                    parms.Add(RfcConfigParameters.PoolSize, "5")
                    parms.Add(RfcConfigParameters.MaxPoolSize, "10")
                    parms.Add(RfcConfigParameters.IdleTimeout, "600")
    
                Case Else
    
            End Select
    
            Return parms
    
        End Function
    
        Public Function ChangeEventsSupported() As Boolean Implements IDestinationConfiguration.ChangeEventsSupported
            Return False
        End Function
    
    End Class

     

    Then, create a web application, console application, web service, whatever, that uses the NCo 3.0 object model.  Very simple stuff to call an RFC enabled Function Module.  See sample Console application below:

     

    Imports SAP.Middleware.Connector
    
    Module Driver
    
        Private _ecc As RfcDestination
    
        Sub Main()
    
            RfcDestinationManager.RegisterDestinationConfiguration(New ECCDestinationConfig)
    
            Try
    
                _ecc = RfcDestinationManager.GetDestination("ECDCLNT140")
    
                GetCompanyName()
    
    
            Catch ex As Exception
                System.Console.WriteLine(ex.Message)
                System.Console.ReadLine()
            End Try
    
    
        End Sub
    
        Private Sub GetCompanyName()
            System.Console.WriteLine(String.Format("Successfully connected to System {0} Client {1}.", _ecc.SystemID, _ecc.Client))
            System.Console.WriteLine("Enter a company ID:")
    
            Dim companyID As String = System.Console.ReadLine()
    
            While Not String.IsNullOrEmpty(companyID.Trim)
    
                Dim companyAPI As IRfcFunction = _ecc.Repository.CreateFunction("BAPI_COMPANY_GETDETAIL")
                companyAPI.SetValue("COMPANYID", companyID)
    
                companyAPI.Invoke(_ecc)
    
                Dim companyName As String = companyAPI.GetStructure("COMPANY_DETAIL").GetString("NAME1")
    
                If String.IsNullOrEmpty(companyName.Trim) Then
                    companyName = "Not found"
                End If
    
                System.Console.WriteLine(companyName)
    
                companyID = System.Console.ReadLine()
    
            End While
        End Sub
    
    End Module

     

    Pretty sweet how you don't have to define the input or output structures.  It's all done behind the scenes.  Love it!

    • Re: Call BAPI using SAP .NET Connector 3.0
      Enrico Zerilli
      Currently Being Moderated

      Hi

      how i can call a BAPI from a simple .NET Client using SAP.Net Connector 3.0?

      There is a simple sample that show that?

       

      Thanks you in advance

    • Re: Call BAPI using SAP .NET Connector 3.0
      Kiran Pedda
      Currently Being Moderated

      Hi Case,

                I see the post has been quite old . Your code sample looks nice and easy.

      One thing on the Nco 3.0 connector about hte way it uses the function metadata at run time (using the statement, IRfcFunction objrfc = rep.CreateFunction("ZSOME_TEST") ), Every time this statement is called on various Rfc function names it takes couple of seconds to get the instance of IRfcFunction. When i know my Rfc functions won't change a lot, is there a way to store this metadata some how at design time (not like the design time proxy generation in Nco 2.0)?

      The time delay caused in this statement is deteriorating the performance of my .net app. Rest of all the process, of Invoking the actual Rfc function and downloading/ uploading the data goes pretty quickly in not more than a second). Any thoughts around this could be helpful..!

       

      Regards,

      KP

  • Re: Call BAPI using SAP .NET Connector 3.0
    Case Ahr
    Currently Being Moderated

    I neglected to mention that you must reference "sapnco.dll" in your project.  Also, make sure your target framework is ".NET v4.0" NOT ".NET v4.0 Client" -- that way you get the System.Web namespace that is required.

    • Re: Call BAPI using SAP .NET Connector 3.0
      ASHISH GADEKAR
      Currently Being Moderated

      Hello Expert,

      When I use bellow code, no values display in my webform, can you help me to find where am goes wrong.

       

           try

                  {

                     

                      RfcDestinationManager.RegisterDestinationConfiguration(new Class1());

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

                      RfcRepository myrepository = mydestination.Repository;

                      IRfcFunction Bapiemployeelist = myrepository.CreateFunction("BAPI_EMPLOYEE_GETDATA");

       

       

                      Bapiemployeelist.SetValue("EMPLOYEE_ID", TextBox1.Text.Trim());

                      Bapiemployeelist.Invoke(mydestination);

                     

                      IRfcTable Employeetable = Bapiemployeelist.GetTable("PERSONAL_DATA");

       

       

                      if (Employeetable.RowCount>0)

                      {

                          TextBox2.Text = Employeetable.GetString("PERNO").ToString();

                          TextBox3.Text = Employeetable.GetString("LAST_NAME").ToString();

                          TextBox4.Text = Employeetable.GetString("FIRSTNAME").ToString();

                          TextBox6.Text = Employeetable.GetString("COSTCENTER").ToString();

                      }

       

       

                  }

                  catch (RfcInvalidParameterException exec)

                  {

                      Label1.Text = exec.Message;

                  }

                  catch (RfcTypeConversionException exec2)

                  {

                      Label1.Text = exec2.Message;

                  }

                  catch (RfcCommunicationException exec1)

                  {

                      Label1.Text = exec1.Message;

                  }

                  catch (RfcBaseException exec3)

                  {

                      Label1.Text = exec3.Message;

                  }

                 

              }

          }

          }

  • Call BAPI using SAP .NET Connector 3.0
    ASHISH GADEKAR
    Currently Being Moderated

    Shafi, .net connector 3 works with .net framework4, try it in vs 2010.