Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SAP
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.
 
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 9-Jul-13 22:48pm
Edited 9-Jul-13 22:49pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 :
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[^]
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100