Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I am using COM component from dot net to connect SAP and access one of function from SAP, for your reference i have provided my test code, for this code i am using Visual Studio 2010.

C#
using SAPLogonCtrl;
using SAPFunctionsOCX;

Public void TestCode()
{
 SAPLogonCtrl.Connection SapConnection = default(SAPLogonCtrl.Connection);
 SAPFunctionsOCX.SAPFunctions SapFns = new SAPFunctionsOCX.SAPFunctions();

 SAPLogonCtrl.SAPLogonControl SapLogonCtrl = new SAPLogonCtrl.SAPLogonControl();
 SapLogonCtrl.ApplicationServer = MyApplnServer;
 SapLogonCtrl.Client = MyClient;
 SapLogonCtrl.User = User;
 SapLogonCtrl.Password = Pwd;
 SapLogonCtrl.Language = "EN";

 SapFns.Connection = SapConnection;//<-- Getting error BAD VARIANT TYPE when using through Web Service
 SAPFunctionsOCX.Function SapFunctionInterface = default(SAPFunctionsOCX.Function);
 SapFunctionInterface = (SAPFunctionsOCX.Function)SapFns.Add(FM);
}


When i use through Window application all work fine but when same code used from Web Service it give me "BAD VARIANT TYPE" error (at the time of setting SapConnection to SapFns.Connection)...

Please help me regarding where i am going wrong or any link which will help me for same issue...

Thanks and Regards,
Santosh Sawant
Posted
Updated 9-Jul-13 21:49pm
v2

1 solution

Dear Friends,

Hurrreeey......

I have found the solution for the BAD VARIANT TYPE issue...

The SAP system is single thread where as when we call it we have to take care of this important requirement, when we try to connect SAP using Window application it will work cause of Single Thread created on each event but when we create Window Service it is not single threaded so we have to change the event to single thread which could be possible by explicitly make call to this method as single thread, frankly speaking i got this code after search on google (find the below url for your reference), in this url the scenario is difference but this give me direction how to explicitly do single thread.

My Code :
XML
var staThread = new System.Threading.Thread(() => MyMethod());
            staThread.SetApartmentState(System.Threading.ApartmentState.STA);
            staThread.Start();
            staThread.Join();


This above code help the method to work as single thread and the connection is establish without any error.

Thanks Romoku for your answer in below url which help me lot.

Google URL for the example :
http://stackoverflow.com/questions/16146022/windows-service-the-calling-thread-cannot-access-this-object-because-a-differen[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900