cancel
Showing results for 
Search instead for 
Did you mean: 

How do I catch a returned RfcTable using SAP .NET Connector 3.0?

bryan_lanning2
Explorer
0 Kudos
1,417

I am using the SAP .NET Connector 3.0 (.NET Framework 4.0 64-bit) with Visual Studio 2010 Pro on a Windows 7 64-bit computer. I have successfully run the example application from the SAP .NET Connector 3.0 Overview that returned a single company in an exporting variable as a structure and then displayed a single field from the structure.

What I need to be able to do is get a list (table) of the sales orders and work with the rows and fields within the rows. Modifying the example from the Overview document, I tried the following:

static void Main(string[] args)
        {
            RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());
            RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");
            try
            {
                RfcRepository repo = prd.Repository;
                IRfcFunction salesOrderBapi = repo.CreateFunction("BAPI_SALESORDER_GETLIST");
                salesOrderBapi.SetValue("CUSTOMER_NUMBER", "246");
                salesOrderBapi.SetValue("SALES_ORGANIZATION", "XXXX");
                salesOrderBapi.Invoke(prd);
                //IRfcTable table = companyBapi.GetTable("SALES_ORDERS");
                IRfcTable table = salesOrderBapi["SALES_ORDERS"].GetTable();
                Console.WriteLine("The table has {0} rows.", table.RowCount);

                foreach (IRfcStructure row in table)
                {
                    String field = row.GetString("SD_DOC");
                    Console.WriteLine(field);
                }
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.Write("There was an error... {0}", ex.Message);
                throw;
            }
        }

When I execute the function from within the SAP GUI with the given parameters, I get a table with 3 rows in it (which is what I expect from what I know of the data). When I execute it from the .NET program, it says that there are 0 rows and the foreach loop doesn't have anything to loop over so I don't know if that portion will even work.

Can someone point out what I'm doing wrong?? Thanks in advance.

View Entire Topic
bryan_lanning2
Explorer
0 Kudos

The problem was in the parameter value that I was passing for the CUSTOMER_NUMBER. I originally had the following:

salesOrderBapi.SetValue("CUSTOMER_NUMBER", "246");

During debugging, I wrote out the Metadata (salesOrderBapi.Metadata.ToString()) and saw the following:

FUNCTION BAPI_SALESORDER_GETLIST (
EXPORT RETURN:STRUCTURE BAPIRETURN, 
IMPORT CUSTOMER_NUMBER:CHAR10,
IMPORT DOCUMENT_DATE:DATE [optional:null], 
IMPORT DOCUMENT_DATE_TO:DATE [optional:null], 
IMPORT MATERIAL:CHAR18 [optional:null], 
IMPORT MATERIAL_EVG:STRUCTURE BAPIMGVMATNR [optional:null], 
IMPORT PURCHASE_ORDER:CHAR20 [optional:null], 
IMPORT PURCHASE_ORDER_NUMBER:CHAR35 [optional:null], 
IMPORT SALES_ORGANIZATION:CHAR4, 
IMPORT TRANSACTION_GROUP:CHAR1 [optional:0], 
TABLES SALES_ORDERS:STRUCTURE BAPIORDERS)

Note the third line where it defines the CUSTOMER_NUMBER as CHAR10. I was only passing 3 characters. Once I padded the customer number with zeroes, it worked like a champ. Here's the resulting code that prints out the SD_DOC field for each of the rows in the returned table:


        static void Main(string[] args)
        {
            RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());
            RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");
            try
            {
                RfcRepository repo = prd.Repository;
                IRfcFunction soBapi = repo.CreateFunction("BAPI_SALESORDER_GETLIST");
                soBapi.SetValue("CUSTOMER_NUMBER", "0000000246");
                soBapi.SetValue("SALES_ORGANIZATION", "XXXX");
                soBapi.Invoke(prd);
                IRfcTable table = soBapi["SALES_ORDERS"].GetTable();
                Console.WriteLine("The table has {0} rows.", table.RowCount);
                foreach (IRfcStructure row in table)
                {
                    String field = row.GetString("SD_DOC");
                    Console.WriteLine(field);
                }
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.Write("There was an error... {0}", ex.Message);
                throw;
            }
        }

Hopefully this explanation helps out someone else who may be trying the .NET Connector 3 for the first time!