cancel
Showing results for 
Search instead for 
Did you mean: 

How to Get Table Data Using NCo 3.0 in c#

Former Member
0 Kudos

Hi,

I am new to SAP NCo 3.0 and starting off. Can anyone share a sample code to get the data from SAP Table and show it in a Gridview in C#.NET

Thanks

Snehal

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

In my projects I convert from IRfcTable to DataTable with little extension method .
This is part of small dll written by me with some helpful utility classes and extension methods for easy dynamic access.

public static class SapToDataExtensionClass

{

public static DataTable GetDataTable(this IRfcTable i_Table)

{

DataTable dt = new DataTable();

dt.GetColumnsFromSapTable(i_Table);

dt.FillRowsFromSapTable(i_Table);

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);

}

}

phil_soady
Participant
0 Kudos

Nice extension Method Yarden. 

I still find it hard to believe teh community needs to build the most obvious extensions just to use the connector.   , ILIST... IBINDING... ICOLLECTION interfaces if implemented properly should result is a valid DataSource for a Control.

Amazing that the dont and the sample code is using FORMS and not WPF.

Im I wrong in seeing this as a .net CO bug / Incorrection interface implementation?

Former Member
0 Kudos

//Declare Class

  public class BankList
    {
        public string bankCtry { get; set; }
        public string bankKey { get; set; }
        public string bankName { get; set; }
        public string city { get; set; }
        public string region { get; set; }
        public string bankNo { get; set; }

    }

//Method to get Data From SAP

public List<BankList> getSAP(String search)
        {
           
            string param = "";
            param = search.ToString();
                try
                {
                    RfcDestination prd = RfcDestinationManager.GetDestination("IDES");
                    BankList objBankList = new BankList();
                    List<BankList> objBank = new List<BankList>();
                    RfcRepository repo = prd.Repository;
                    IRfcFunction companyBapi = repo.CreateFunction("BAPI_BANK_GETLIST");
                    companyBapi.SetValue("BANK_CTRY", param);
                    companyBapi.Invoke(prd);

                  
                    List<IRfcStructure> irc=new List<IRfcStructure>();
                    IRfcTable detail = companyBapi["BANK_LIST"].GetTable();

                    List<string> cnt = new List<string>();


                    foreach(IRfcStructure ir in detail)
                    {
                      
                        objBank.Add(new MyBackendConfig().Insert(ir));

                    }

                    repo.ClearTableMetadata();

                    return objBank;
                  
                }
             catch (Exception ex)
                {
                    return null;
                }
        }

//MyBackendConfig().Insert(ir) Declaration

public BankList Insert(IRfcStructure ir)
     {
         BankList bklist = new BankList();
         bklist.bankCtry = ir.GetString("BANK_CTRY");
         bklist.bankName = ir.GetString("BANK_NAME");
         bklist.bankKey = ir.GetString("BANK_KEY");
         bklist.city = ir.GetString("CITY");

         return bklist;

     }

//Attach Datasource to grid

gvRtMasterUI.DataSource = MBC.getSAP(countrySearch);

gvRtMasterUI.DataBind();

Former Member
0 Kudos

Dear Swapnil,

please find below code, This one successfully  executed and integrated into my project.

///Below code is useful to configure the server connection string from .net. Here i am taking sample connection parameters.

///Below code is useful to configure the server connection string from .net. Here i am taking sample connection parameters.

public class MyBackendConfig : IDestinationConfiguration

        {

            // RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());

            public RfcConfigParameters GetParameters(String destinationName)

            {

                if ("SE37".Equals(destinationName))

                {

                    RfcConfigParameters parms = new RfcConfigParameters();

                    parms.Add(RfcConfigParameters.AppServerHost, "192.168.1.14"); 

                    parms.Add(RfcConfigParameters.SystemNumber, "02"); 

                    parms.Add(RfcConfigParameters.User, "abaper"); 

                    parms.Add(RfcConfigParameters.Password, "erp@1234"); 

                    parms.Add(RfcConfigParameters.Client, "800"); 

                    parms.Add(RfcConfigParameters.Language, "EN");

                    parms.Add(RfcConfigParameters.PoolSize, "5"); 

                    parms.Add(RfcConfigParameters.MaxPoolSize, "10");

                    parms.Add(RfcConfigParameters.IdleTimeout, "600"); 

                    return parms; 

                }

                else return null; 

            }

            public bool ChangeEventsSupported()

            {

                return false;

            }

            public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

            // public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; 

        }

    } 

// In page_load event add this below code for regitering the server settings. 

step4:       MyBackendConfig config__ = new MyBackendConfig(); 

                try

                { 

               RfcDestinationManager.RegisterDestinationConfiguration(config__); 

                }

                catch (RfcInvalidStateException exec)

                {

                     Label1 .Text= exec.Message;

                 } 

                catch (RfcCommunicationException exec2)

                {                    Label1.Text = exec2.Message;

                } 

Step5 : How to Read the values from sap database and displayed into ASP.net Gridview control. 

//here i am taking the example for displaying the employee details based upon the empid. 

//In show button click copy this code. 

//Note:  Ask BAPI names, IMOPRT paramaters and EXPORT parameters from ABAPERS.Here i am taking sample bapi names and parametrs.

                RfcDestination prd = RfcDestinationManager.GetDestination("SE37");

                RfcRepository repo = prd.Repository; 

              // BAPI_ADDREMPAU_GETDETAIL is bapi please take bapi name from abaper. 

               IRfcFunction companyBapi1 = repo.CreateFunction("BAPI_ADDREMPAU_GETDETAIL"); 

             // set the employeenumber based upon the employeenumber we need to display empdetails.

               companyBapi1.SetValue("EMPLOYEENUMBER", int.Parse(txtEmploeeNumber.Text)); 

             // Invoke the rfcfunction

             companyBapi1.Invoke(prd);

                for (int i = 0; i < companyBapi1.ElementCount; i++)

                {

                    RfcDirection direction = companyBapi1[i].Metadata.Direction;

                    if (companyBapi1[i].Metadata.DataType.ToString().Trim() != "STRUCTURE" && direction.ToString().Trim() =="EXPORT")

                    {

                        dt.Columns.Add(companyBapi1.Metadata[i].Name);         

           }

                }

                DataRow dr = dt.NewRow();

                dt.Rows.Add(dr);

                int columnnumber = 0;

                for (int m = 0; m < companyBapi1.Count; m++)

                {

                    RfcDirection direction = companyBapi1[m].Metadata.Direction;

                    if (companyBapi1[m].Metadata.DataType.ToString().Trim()!= "STRUCTURE" && direction.ToString().Trim() == "EXPORT" && columnnumber<dt.Columns.Count)

                    {

                        dt.Rows[0][columnnumber] = companyBapi1[m].GetString();

                    }

                    columnnumber++;

                }

            }

catch (RfcInvalidParameterException exec)

            {

                Label1.Text = exec.Message;

            }

            catch (RfcTypeConversionException exec1)

            {                Label1.Text = exec1.Message;

            }

            catch (RfcCommunicationException exec2)

            {

              Label1.Text = exec2.Message;

            }

            catch (RfcBaseException exec3)

            {

            Label1.Text = exec3.Message;

            }

            GridView1.DataSource = dt;

            GridView1.DataBind();

    }

}

Best Regards,

Harish.Y

Former Member
0 Kudos

Sorry Former Member

I have a question, where did you define the dt variable? (data table variable)