5

I am developing an windows application, here I wanna extract data from sap system and show it in a datagridview... I have extracted the column names alone like name, city and so on..

I don't know how to extract the data from the columns, can anyone help me with the code?

I am using RFC_READ_TABLE function module and rfc destiantion manager

Thanks in advance!!!

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Jeswin Rebil
  • 460
  • 1
  • 7
  • 19
  • I have extracted the field names from the table.. Now i want to extract the values from each column which holds nearly 5000 entries – Jeswin Rebil Mar 19 '14 at 11:03

2 Answers2

9

untested, but this is essentially how it works:

first create the connection

RfcDestination destination = mDestinationManager.GetDestination("MYDESTINATION");

create the function

IRfcFunction readTable = destination.Repository.CreateFunction("RFC_READ_TABLE");

set parameters before invoking the function

// we want to query table KNA1
readTable.SetValue("QUERY_TABLE", "KNA1");
// fields will be separated by semicolon
readTable.SetValue("DELIMITER", ";");

table parameters are created by retrieving the table from the function, using the Append() function to add a row and using SetValue() to set values for individual columns in that row

// Parameter table FIELDS contains the columns you want to receive
// here we query 2 fields, KUNNR and NAME1
IRfcTable fieldsTable = readTable.GetTable("FIELDS");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "KUNNR");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "NAME1");

// the table OPTIONS contains the WHERE condition(s) of your query
// here a single condition, KUNNR is to be 0012345600
// several conditions have to be concatenated in ABAP syntax, for instance with AND or OR
IRfcTable optsTable = readTable.GetTable("OPTIONS");
optsTable.Append();
optsTable.SetValue("TEXT", "KUNNR = '0012345600'");

call the function

readTable.Invoke(destination);

process the data

IRfcTable dataTable = readTable.GetTable("DATA");

foreach(var dataRow in dataTable)
{
    string data = dataRow.GetValue("WA");
    string[] columns = data.Split(';');
}
Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • This is the code I have already,Your code is absolutely correct, but I found out that the table which I cald has no entries, that's the mistake I made, now I cald another table it works fine... Thank you so much for taking an initiative.. Now I face data buffer exceeds in some table, I don't know why, if u can help me plz – Jeswin Rebil Mar 20 '14 at 06:34
  • 1
    RFC_READ_TABLE has some limitations. There once was a SAP note describing a modification to RFC_READ_TABLE (copy the whole function module into customer namespace and change it there) that got rid of some limitations. This SCN thread describes the changes: http://scn.sap.com/thread/338270 . – Dirk Trilsbeek Mar 20 '14 at 07:18
  • I can't understand that, if u can plz tel me what I have to do, to avoid that error? my table name is PA0002 – Jeswin Rebil Mar 20 '14 at 09:00
  • the function module you use has certain limitations (512 char width) and can cause problems with certain column types and in SAP Unicode systems. I am not aware of a SAP standard function module that you could use instead, so you will have to make your own (within the SAP system, using ABAP). The link in my previous comment leads to a short description on how to modify a copied version of RFC_READ_TABLE. If you can't do that in the SAP system yourself you might have to ask an ABAP developer to do it for you. Then you call that new function module instead of RFC_READ_TABLE and it should work. – Dirk Trilsbeek Mar 20 '14 at 09:14
  • yeah I can understand very clearly now..... Thanks a lot mate, I wanna inform this to my sap people and wanna do something... Because of my less reputation I can't even vote for the post :( – Jeswin Rebil Mar 20 '14 at 11:44
  • Thank you for this concise code example. It helped me out big time! – Sagi Feb 11 '15 at 08:35
0

For RFC_READ_TABLE you also may want to consider the free .net ExtracTable tool on sourceforge.net It uses RFC_READ_TABLE and will report into Excel or CSV file. As RFC_READ_TABLE may give you problems for records having more than 512 characters, tool above takes care. Or you would have to program your own split&join routine for such SAP tables. But such always comes with pros&cons.