cancel
Showing results for 
Search instead for 
Did you mean: 

The Problem with NCO 3 and C# IN Visual Studio 2010 Express

Former Member
0 Kudos

Dear Experts:

I have a proplem with NCO 3 and C# IN Visual Studio 2010 Express.

To run C# ,I use Visual Studio 2010 and the steps as follows:

1.Create a new project IN VS 2010.

(1)ADD a new projet

(2)select windows form application in C#

(3)select Form1.cs and double click it.

2. Write codes in Form1.cs the code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SAP.Middleware.Connector;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        string MATNR = string.Empty;

        public Form1()
        {

            InitializeComponent();

        }

        public void nco()
        {

            IDestinationConfiguration ID = new MyBackendConfig();

            RfcDestinationManager.RegisterDestinationConfiguration(ID);

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

            RfcDestinationManager.UnregisterDestinationConfiguration(ID);

            nco(prd);

        }

        public void nco(RfcDestination prd)
        {

            RfcRepository repo = prd.Repository;

            IRfcFunction companyBapi = repo.CreateFunction("ZRFC_MARA_INFO");   //RFC FUNCTION NAME

            companyBapi.SetValue("MATNR", MATNR);   //SET Import PARAMETER

            companyBapi.Invoke(prd);   //  EXECUTE FUNCTION

            IRfcTable table = companyBapi.GetTable("IT_MARA");  //GET MAPPING INTERAL TABLE
            string MAKTX = companyBapi.GetValue("MAKTX").ToString();  //GET MM NAME
            DataTable dt = new DataTable();  //CREATE A NEW TABLE

            dt.Columns.Add("u54C1u53F7");  //ADD A COLUMN
            for (int i = 0; i < table.RowCount; i++)
            {

                table.CurrentIndex = i;  //CURRENT INDEX
                DataRow dr = dt.NewRow();

                dr[0] = table.GetString("MATNR");  //  GET    TABLE  VALUE    IN  Specific   ROWS AND CLOS u83B7u53D6u8868u683Cu7684u67D0u884Cu67D0u5217u7684u503C

                dt.Rows.Add(dr);  // fill value in the table  u586Bu5145u8BE5u8868u683Cu7684u503C

            }

            if (MATNR == "")
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    this.comboBox1.Items.Add(dt.Rows<i>[0].ToString());   //u586Bu5145u4E0Bu62C9u6846

                }

            }

            this.label1.Text = MAKTX;   //u663Eu793Au54C1u540D

            prd = null;

            repo = null;

        }


        //u767Bu9646SAPu524Du7684u51C6u5907u5DE5u4F5C

        public class MyBackendConfig : IDestinationConfiguration
        {

            public RfcConfigParameters GetParameters(String destinationName)
            {

                if ("PRD_000".Equals(destinationName))
                {

                    RfcConfigParameters parms = new RfcConfigParameters();

                    parms.Add(RfcConfigParameters.AppServerHost, "192.168.2.22");   //SAP HOST IP

                    parms.Add(RfcConfigParameters.SystemNumber, "00");  //SAP  instance No.
 
                    parms.Add(RfcConfigParameters.User, "hi");  //login name

                    parms.Add(RfcConfigParameters.Password, "demo");  //login password

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

                    parms.Add(RfcConfigParameters.Language, "ZH");  // laguage

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

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

                   // parms.Add(RfcConfigParameters.IdleTimeout, "60");

                    return parms;

                }

                else return null;

            }

            public bool ChangeEventsSupported()
            {

                return false;

            }

            public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;

        }


        private void Form1_Load(object sender, EventArgs e)
        {

            comboBox1.Items.Clear();

            nco();

            comboBox1.SelectedIndex = 1;

        }

        //u5F53u4E0Bu62C9u6846u7D22u5F15u53D8u5316u7684u65F6u5019u4F20u9012u54C1u53F7u8FDBu53BBu67E5u8BE2u51FAu54C1u540Du51FAu6765

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            MATNR = comboBox1.Text.ToString();

            nco();

        }

    }


}

When running, the error messages show as follow:

RfcDestinationManager.RegisterDestinationConfiguration(ID);---> Type inSystem.TypeInitializationException

Can any experts help me to slove the problem.

By the way, All things should be created in sap, I have created ,ex: RFC..,etc.

Edited by: jetsap on Dec 5, 2011 9:56 AM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Jet,

Not quite sure, but I think that if you change the following line:

IDestinationConfiguration ID = new MyBackendConfig();

to MyBackendConfig ID = new MyBackendConfig();

you will probably don't get that error.

regards

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

I am giving a small example. Plz try in this way

public class MyBackendConfig : IDestinationConfiguration

{

public RfcConfigParameters GetParameters(String destinationName)

{

if ("SE38".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;

}

private void Form1_Load(object sender, EventArgs e)

{

RfcDestinationManager.RegisterDestinationConfiguration(new MyBackendConfig());

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

RfcRepository repo = prd.Repository;

IRfcFunction companyBapi = repo.CreateFunction("ZBAPI_PACKAGE_GETLIST");

companyBapi.Invoke(prd);

IRfcTable address = companyBapi["ZPACKAGE"].GetTable();

for (int index = 0; index < address.RowCount; ++index)

{

comboBox1.Items.Add(address[index]["DEVCLASS"].GetString());

}

}