cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get a Transparent Table or Internal Table from SAP with SAP Connector RFC

Former Member
0 Kudos

Hello,

I want to know How can I get a table from SAP, using .Net connector from VB.NET or C# and using this table with .net controls like ComboBox, Gridview or Datatable. to show the table content in a combo box o gridview, is this possible?

with .NET Connector we can get a structure, but a structure only has one row:

string GetData = Data.GetStructure("WA_PAPEL").GetString("Country");

But with a table?

Accepted Solutions (1)

Accepted Solutions (1)

former_member197445
Contributor
0 Kudos

The Function should have either a "Tables" argument or a table type output.  An example BAPI to look at is BAPI_MATERIAL_GETLIST, which has many table arguments which you can access.  On the .NET side, simple call "GetTable" to get an IRfcTable object. 

            //Set function

            IRfcFunction BapiMaterialGetList =

              SapRfcRepository.CreateFunction("BAPI_MATERIAL_GETLIST");

            //Get reference to table object

            IRfcTable So_MATNR = BapiMaterialGetList.GetTable("MATNRSELECTION");

After that you can convert it to a DataTable to set your control DataSource to.

Convert to datatable function:


public static DataTable GetDataTableFromRFCTable(IRfcTable functionRfcTable)
{

    DataTable data = new DataTable();

    //Create data table.
    for (int i = 0; i <= functionRfcTable.ElementCount - 1; i++) {
        RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(i);
        data.Columns.Add(metadata.Name);
    }

    //Transfer rows from rfcTable to .Net table.
    foreach (IRfcStructure row in functionRfcTable) {
        DataRow rowAdd = data.NewRow();
        for (int j = 0; j <= functionRfcTable.ElementCount - 1; j++) {
            RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(j);
            rowAdd(metadata.Name) = row.GetString(metadata.Name);
        }
        data.Rows.Add(rowAdd);
    }

    return data;
}

Former Member
0 Kudos

Hello, thanks for your fast answer, but I have some issues, when I try to run this code, visual studio shows some errors:

  • 'rowAdd' is a 'variable' but is used like a 'method'
  • The name 'SapRfcRepository' does not exist in the current context
  • Class 'ECCDestinationConfig' must implement 'Event ConfigurationChanged(destinationName As String, args As RfcConfigurationEventArgs)' for interface 'SAP.Middleware.Connector.IDestinationConfiguration'.

Could you please help me with this issue?

My complete code below:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using SAP.Middleware.Connector;

using System.Data;

public partial class _Default : System.Web.UI.Page

{

    RfcDestination _ecc;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (Page.IsPostBack)

            try

            {

                _ecc = RfcDestinationManager.GetDestination("ECDCLNT140");

                if (_ecc == null)

                {

                    RfcDestinationManager.RegisterDestinationConfiguration(new ECCDestinationConfig());

                    _ecc = RfcDestinationManager.GetDestination("ECDCLNT140");

                }

           

            }

            catch (Exception a)

            {

                Response.Write(a);

            }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        //Set function

        IRfcFunction BapiMaterialGetList = SapRfcRepository.CreateFunction("BAPI_MATERIAL_GETLIST");

        //Get reference to table object

        IRfcTable So_MATNR = BapiMaterialGetList.GetTable("MATNRSELECTION");

    }

    public static DataTable GetDataTableFromRFCTable(IRfcTable functionRfcTable)

    {

        DataTable data = new DataTable();

        //Create data table.

        for (int i = 0; i <= functionRfcTable.ElementCount - 1; i++)

        {

            RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(i);

            data.Columns.Add(metadata.Name);

        }

        //Transfer rows from rfcTable to .Net table.

        foreach (IRfcStructure row in functionRfcTable)

        {

            DataRow rowAdd = data.NewRow();

            for (int j = 0; j <= functionRfcTable.ElementCount - 1; j++)

            {

                RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(j);

                rowAdd(metadata.Name) = row.GetString(metadata.Name);

            }

            data.Rows.Add(rowAdd);

        }

        return data;

    }

}

former_member197445
Contributor
0 Kudos

Sorry!  I work mostly in VB. 

  • rowAdd(metadata.Name) should be
    rowAdd[metadata.Name]

  • Your repository would be _ecc.Repository... I used SapRfcRepository just an example.
    _ecc.Repository.CreateFunction("BAPI_MATERIAL_GETLIST");

  • Looks like you haven't set up your RFC Destination correctly.  See this post for an example.  http://scn.sap.com/thread/1870950.  Note: you must use your own system identifiers and client numbers.

Former Member
0 Kudos

Thanks!

abusing of your hospitality, I've one issue, but I don't know why c# show me this error:

  • User code not control NullReferenceException
  • Object reference not set to an instance of an object

I have worked with VB.NET to get an structure and it's very easy to solve with this code in my load page:

   Try

            _ecc = RfcDestinationManager.TryGetDestination("ECDCLNT140")

            If (_ecc Is Nothing) Then

                RfcDestinationManager.RegisterDestinationConfiguration(New ECCDestinationConfig())

                _ecc = RfcDestinationManager.GetDestination("ECDCLNT140")

            End If

            ' GetCompanyName()

            'TextRolloNum.Text = ""

            TextRolloNum.Focus()

        Catch ex As Exception

        End Try

But in My C# I have this:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (Page.IsPostBack)

            try

            {

                _ecc = RfcDestinationManager.GetDestination("ECDCLNT140");

                if (_ecc == null)

                {

                    RfcDestinationManager.RegisterDestinationConfiguration(new ECCDestinationConfig());

                    _ecc = RfcDestinationManager.GetDestination("ECDCLNT140");

                }

            }

            catch (Exception a)

            {

                Response.Write(a);

            }

    }

Do you know what I'm doing wrong?

Do you have an example with vb.net that does work  well?

Thanks.

Answers (0)