The sample code is located in the ERPConnect installaltion directory in the ReadSAPTable directory

A recurrent task in daily work with SAP and .Net applications is to read directly from tables of the SAP system. You can use the ReadTable class to manage this demand.

The sample below shows how to select data from the table. The result is passed back via an easy-to-use ADO.NET table object.

In this sample we want to read material description texts, which are located in the table MAKT. So we need the two columns MATNR (material number) and MAKTX (material description).

Furthermore we want only the English texts so we have to add the WHERE statement SPRAS=’EN’. SPRAS is the column which contains the language keys.

The method Run executes the query and passes back the ADO.NET table.

Click to open C# example.
using System; 
using ERPConnect; 
using ERPConnect.Utils; 
using System.Data; 
     
class Class1
{ 
   static void Main(string[] args) 
   { 
        using(R3Connection con = new R3Connection("hamlet",11,"theobald","pw","DE","800"))
        {
            con.Open(false);
            ReadTable table = new ReadTable(con); 
            table.AddField("MATNR"); 
            table.AddField("MAKTX"); 
            table.AddCriteria("SPRAS = 'EN'");
            table.AddCriteria("AND MATNR LIKE '%23'");
            table.TableName = "MAKT"; 
            table.RowCount = 10; 
         
            table.Run(); 
         
            DataTable resulttable = table.Result; 
         
            for(int i=0; i < resulttable.Rows.Count;i++) 
            { 
                Console.WriteLine( 
                 resulttable.Rows[i]["MATNR"].ToString() + " " + 
                 resulttable.Rows[i]["MAKTX"].ToString()); 
            }
          
            Console.ReadLine(); 
        }
    }
}
Click to open VB example.
Module Module1 
     
   Sub Main() 
     
      Using con As New R3Connection 
          con.Host = "Hamlet"
          con.SystemNumber = 11 
          con.UserName = "Theobald"
          con.Password = "pw"
          con.Client = "800"
          con.Language = "DE"
         
          con.Open(False) 
         
          Dim table As New ReadTable(con) 
         
          table.AddField("MATNR") 
          table.AddField("MAKTX") 
          table.AddCriteria("SPRAS = 'EN'")
          table.AddCriteria("AND MATNR LIKE '%23'")
                  
          table.TableName = "MAKT"
         
          table.RowCount = 10 
         
           table.Run() 
         
           Dim resulttable As DataTable resulttable = table.Result 
         
           Dim i As Integer
           For i = 0 To resulttable.Rows.Count - 1 
              Console.WriteLine( _ CStr(resulttable.Rows(i)(0)) + " " + _ 
                 CStr(resulttable.Rows(i)(1))) 
           Next
         
           Console.ReadLine() 
        End Using
   End Sub
End Module

The screenshot shows the output of the sample program.

ReadTable-Console

Table Access Restrictions

Unfortunately following restrictions apply if you read tables with the ReadTable class:

  • Tables that should be read from may not have any floating point number (FLTP) type of column (as in the case of e.g. the VBAK table).
  • The overall width of the columns to be extracted may not exceed 512 bytes.
  • Some special tables like TCURR return false values.

If anyone of these cases occur, the table call will throw an exception. To deal with this problem, it is possible to install a Z-module in the SAP system, please see chapter Installing the Z-function module.

To enable an installed Z-module, e.g. Z_XTRACT_IS_TABLE, when using the ReadTable class, use the method

ReadTable.SetCustomFunctionName(“Z_XTRACT_IS_TABLE”).

links

Online Help: Installing the custom function module

KnowledgeBase: Transferring data packets with ReadTable class

KnowledgeBase: Get meta data of a table