Vs08 calls the COM component to connect to sap

Source: Internet
Author: User

 

From http://blog.csdn.net/wren2004/archive/2009/01/22/3849688.aspx

Yesterday we talked about how to use form in vs2005 to call the function module in SAP ecc6.0. Today we will record how to call the function module in vs2005 in the form of web.

 

The function module is called in the web form basically the same as the form. The only thing worth noting is: "an ActiveX control must be run in an sta apartment. because the attribute stathread is applied to the main method of a winform class by default with a winforms project, the main thread of your app will run in Sta mode. ". that is to say, some acrtivex controls or COM components must run in a single thread unit (STA: single thread apartment); otherwise, a "bad variant type" exception will be thrown. Solution: open a new thread, set the running mode of the thread to STA, and then call the COM component or ActiveX control under the thread.

In our case, if we do not run in Sta mode, we can connect to the SAP system, but an "Bad variant type" exception will be thrown when the function module is called. Therefore, the code that calls the function module is executed in the new thread. The procedure is as follows:

1. Add references to the InterOP. sapfunctionsocx. dll, InterOP. saplogonctrl. dll, and InterOP. saptablefactoryctrl. dllcom components.

2. Open a new thread, set the running mode of the thread to STA, and run the methods used to log on to the SAP system and call the function module under the thread! The Code is as follows:

Protected void button#click (Object sender, eventargs E)
{
System. threading. thread S = new system. threading. thread (new system. threading. threadstart (TEST); // create a new thread and set the method test () run in this thread
S. setapartmentstate (system. Threading. apartmentstate. Sta); // set the run mode 'sta'
S. Start (); // start the thread
S. Join (); // wait until thread run OK.
Gridview1.datasource = DT;
Gridview1.databind ();
MSG. Text = "get data from 'enqueue _ read' OK! ";
}

Private void test ()
{
Saplogonctrl. saplogoncontrolclass login = new saplogonctrl. saplogoncontrolclass ();
Login. applicationserver = "";
Login. Client = "";
Login. Language = "en ";
Login. User = username. text;
Login. Password = psw. text;
Login. systemnumber = 00;
Saplogonctrl. Connection conn = (saplogonctrl. Connection) login. newconnection ();

If (conn. Logon (0, true ))
{
Sapfunctionsocx. sapfunctionsclass func = new sapfunctionsocx. sapfunctionsclass ();
Func. Connection = conn;
Sapfunctionsocx. ifunction ifunc = (sapfunctionsocx. ifunction) func. Add ("enqueue_read ");
Sapfunctionsocx. iparameter gclient = (sapfunctionsocx. iparameter) ifunc. get_exports ("gclient ");
Gclient. value = "301 ";
Sapfunctionsocx. iparameter guname = (sapfunctionsocx. iparameter) ifunc. get_exports ("guname ");
Guname. value = "";
Sapfunctionsocx. iparameter local = (sapfunctionsocx. iparameter) ifunc. get_exports ("local ");
Local. value = "0 ";
Ifunc. Call ();
Saptablefactoryctrl. Tables tables = (saptablefactoryctrl. Tables) ifunc. tables;
Saptablefactoryctrl. Table Enq = (saptablefactoryctrl. Table) tables. get_item ("Enq ");
Int n = Enq. rowcount;
Dt = gettable ();
For (INT I = 1; I <= N; I ++)
{
Datarow DR = DT. newrow ();
Dr ["gname"] = Enq. get_cell (I, "gname"). tostring ();
Dr ["guname"] = Enq. get_cell (I, "guname"). tostring ();
Dr ["Garg"] = Enq. get_cell (I, "Garg"). tostring ();
Dr ["gobj"] = Enq. get_cell (I, "gobj"). tostring ();
Dr ["gtdate"] = Enq. get_cell (I, "gtdate"). tostring ();
DT. Rows. Add (DR );
}
}
}

With the above settings, you can call the function module on the web. This method has been tested successfully. Another brother in the garden to another method http://www.cnblogs.com/jirigala/archive/2008/11/26/1341345.html but this method I did not test successfully, do not know where I set the problem or what is going on, if there is a problem please tell, thank you!

 

To supplement the above,

1. If the sap version is not in Unicode mode, do not use a DLL in unicode format.

Run:

Regsvr32 "C: \ Documents and Settings \ Wang. Binbin \ Desktop \ GUI \ windows \ Win32 \ System \ sap \ wdtlog. ocx"

2. If you encounter an SAP data type not supported error, refer to http://msdn.microsoft.com/en-us/library/cc185537 (bts.10). aspx, not all types support RFC, such as string in SAP

 

 

Enter a table:

Ptinheader. appendgriddata (rowcount, (saptablefactoryctrl. Column) cols. get_item ("refno"). index, 1, RW ["accept_no"]. tostring ());//

 

 

 

When using BDC in remote RFC, pay attention to the call transaction mode. Mode 'E' is not allowed. Otherwise, if BDC in RFC is incorrect when an external system calls this RFC, runtime error dynpro_send_in_background has occurred will occur. When a BDC error occurs, mode 'E' is redirected to the error screen, which cannot be passed to the external system for display.
When using BDC in remote RFC, the mode should use 'n' instead of 'E '.

 

 

Chinese garbled characters: Add sap_codepage 8400 to the system variable

Use ZH to log on to the language

If the sap mode is Unicode, you must change the logon mode to Unicode. Otherwise, Chinese garbled characters may occur.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.