vb.net SAP.net connector Step by step guide to get fields from a table BAPI Function GET_RFC_TABLE

Directly access data from  the SAP tables from VB.net

For Visual Studio 2010, you will need to download the latest one:
sapnco30dotnet40P_3-20007347.zip
Unzip and install to a convenient location on your machine.

Create An App

Right click on project and select “Add Reference”
When window opens, select “Browse” and navigate to the folder where you installed the SAP Nco Connector.
You will need to select the following dll:
Sapnco.dll
Sapnco_utils.dll
 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 "DEV"

                parms.Add(RfcConfigParameters.AppServerHost, "10.0.0.47")
                parms.Add(RfcConfigParameters.SystemNumber, "00")
                parms.Add(RfcConfigParameters.SystemID, "ED1")
                parms.Add(RfcConfigParameters.User, "009383")
                parms.Add(RfcConfigParameters.Password, "....")
                parms.Add(RfcConfigParameters.Client, "100")
                parms.Add(RfcConfigParameters.Language, "EN")
                parms.Add(RfcConfigParameters.PoolSize, "5")
                parms.Add(RfcConfigParameters.PeakConnectionsLimit, "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

Write your Form code.. have a textbox and datagridview on form .
Following Code will get 3 fields from a table named PROJ. It also filters the row on a filter criteria using OPTIONS Table

Imports SAP.Middleware.Connector
Public Class Form1
    Private _ecc As RfcDestination
    Public log As String = ""
    Private Sub DisplayMessage(ByVal text As String)
        TextBox1.Text = TextBox1.Text & text
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            RfcDestinationManager.RegisterDestinationConfiguration(New ECCDestinationConfig)

            _ecc = RfcDestinationManager.GetDestination("PROD")  'Put DEV or PROD

          
            '######## GET RFC Table
            Dim itbl = getRFCTable("PROJ", "POST1,PSPNR,PSPID", "ASTNR = 10")
            Dim view As IRfcTableView = TryCast(itbl, ISupportTableView).DefaultView
            DataGridView1.DataSource = view
            '################

       
            DisplayMessage(log)

        Catch ex As Exception
            DisplayMessage("unable to logon " + ex.Message)
        End Try
    End Sub
   

    
    Function getRFCTable(ByVal tblname As String, ByVal commaseperatedfieldnames As String, Optional ByVal filter As String = "") As IRfcTable
        Dim repository As RfcRepository = _ecc.Repository

        ''###########  1. THE MAIN BAPI FUNCTION
        Dim bapiMethod1 As IRfcFunction = repository.CreateFunction("RFC_READ_TABLE")  ''RFC Function
    
        '############  2. Import parameters for BAPI function
        bapiMethod1.SetValue("QUERY_TABLE", tblname)  ''Import
        bapiMethod1.SetValue("DELIMITER", ",")  ''Import

        '############ 3. Set Tables parameters for BAPI function
        Dim field() = Split(commaseperatedfieldnames, ",")
        Dim fieldsTbl As IRfcTable = bapiMethod1.GetTable("FIELDS") '' Table to limit the fields to avoid data excess error

        For Each f In field
            fieldsTbl.Append()
            fieldsTbl.SetValue("FIELDNAME", f)   ' fieldsTbl.SetValue("FIELDNAME", "POST1")
        Next

        '############ 4. Set Table filters for BAPI function
        Dim optionstbl As IRfcTable = bapiMethod1.GetTable("OPTIONS") '' Tables

        filter = Trim(filter)
        Dim x = 1
        Dim q = Len(filter)
        Dim testChar
        Dim temp
        Dim p
        Do While q > 72
            For p = 72 To 0 Step -1
                testChar = Mid(filter, p, 1)
                If InStr(1, " ,)(", testChar) > 0 Then
                    Exit For
                End If
            Next p
            optionstbl.Append()
            temp = Strings.Left(filter, p)
            ' optionstbl.Value(x, "TEXT") =
            optionstbl.SetValue("TEXT", temp)
            x = x + 1
            filter = Mid(filter, p + 1)
            q = Len(filter)
        Loop
        If q > 0 Then
            optionstbl.Append()
            optionstbl.SetValue("TEXT", filter)
        End If
        '  optionstbl.SetValue("TEXT", "ASTNR = 10")
        Dim dataTbl As IRfcTable = bapiMethod1.GetTable("DATA") '' Table  to get data

        ''############ 4. Export  for BAPI function
        'Dim rfcreturn As IRfcStructure = bapiMethod1.GetStructure("RETURN")   ''export

        '############ 5. Now Call for BAPI function
        bapiMethod1.Invoke(_ecc)  '' Call function now limiting the Fields else get data excess error

        log = log & "Returning datatable with Total Rows :" & dataTbl.RowCount.ToString

        For ptr As Integer = 0 To dataTbl.RowCount - 1
            dataTbl.CurrentIndex = ptr
            For i As Integer = 0 To dataTbl.CurrentRow.ElementCount - 1
                log = log & dataTbl.GetString(i) & dataTbl.ElementCount.ToString
            Next
            log = log & vbCrLf
        Next
        '' log = contact
        Return dataTbl
    End Function
End Class


- Written By
Vinod Kotiya

Comments

LaurenHillDev said…
this line will not compile:

Dim view As IRfcTableView = TryCast(itbl, ISupportTableView).DefaultView

it says the type IRfcTableView is not defined.

What do I need to do?
Vinod Kotiya said…
You have not imported sap.net connector dll

do that.