Convert SAP table data into a data table

150 Views Asked by At

We are using SAP .NET Connector (NCo) libraries (SAP.Connector.dll and SAP.Middleware.Connector.dll) to retrieve data from a SAP system using C#. Establishing the connection to the SAP system and retrieving the data is working fine. Below is the sample line of code at the end where we have the SAP table data.

IRfcTable irfcTableColumn = function.GetTable("FIELDS");
IRfcTable irfcTableRow = function.GetTable("DATA");

for "irfcTableColumn" data is coming in below format.

"FIELDS":[
      {
         "FIELDNAME":"MANDT",
         "OFFSET":"000000",
         "LENGTH":"000003",
         "TYPE":"C",
         "FIELDTEXT":"Client"
      },
      {
         "FIELDNAME":"AGRTY",
         "OFFSET":"000004",
         "LENGTH":"000004",
         "TYPE":"C",
         "FIELDTEXT":"AgreementTypeCode"
      },      
      {
         "FIELDNAME":"AEZET",
         "OFFSET":"000147",
         "LENGTH":"000006",
         "TYPE":"T",
         "FIELDTEXT":"SubTypeCode"
      }
   ]

For the Rows, data is coming in the below format.

 "DATA":[
      {
         "WA":"100|DISP|01 "
      },     
      {
         "WA":"100|DISP|05 "
      }
   ]

Now I need a data table in below format

Expected data format

Used below class to convert the data into columns and rows but it is not coming in above expected format. Any help in tweaking the below code to get the expected format is highly appreciated.

dt = SapToDataExtensionClass.GetDataTable(irfcTableColumn, irfcTableRow);

Whole code:

public static class SapToDataExtensionClass
{
public static DataTable GetDataTable(this IRfcTable i_TableC, IRfcTable i_TableR )
{
DataTable dt = new DataTable();
dt.GetColumnsFromSapTable(i_TableC);
dt.FillRowsFromSapTable(i_TableR);
return dt;
}
public static void FillRowsFromSapTable(this DataTAble i_DataTable, IRfcTable i_Table)
{
foreach (IRfcStructure tableRow in i_Table)
{
DataRow dr = i_DataTable.NewRow();
dr.ItemArray = tableRow.Select(structField => structField.GetValue()).ToArray();
i_DataTable.Rows.Add(dr);
}
}
public static void GetColumnsFromSapTable(this DataTable i_DataTable , IRfcTable i_SapTable)
{
var DataColumnsArr = i_SapTable.Metadata.LineType.CreateStructure().ToList().Select
(structField => new DataColumn(structField.Metadata.Name)).ToArray();
i_DataTable.Columns.AddRange(DataColumnsArr);
}
}

I tried with a sample class to convert to my data table format but it is not coming in expected format.

0

There are 0 best solutions below