Hi
I am getting below error on above line -
SAPbouiCOM.Application oApplication = new SAPbouiCOM.Application();
int connectionResult = oCompany.Connect();
if (connectionResult != 0)
{
throw new Exception("Error connecting to SAP Business One: " + oCompany.GetLastErrorDescription());
}
SboGuiApi sboGuiApi = new SboGuiApi();
oApplication = sboGuiApi.GetApplication();
Thanks

Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 12, 2025, 1:06 AM EST
Hello Ramco,
The error you are encountering, "Specified cast is not valid," suggests that the object you are trying to cast does not match the expected type. In your code, the issue likely arises from the line where you attempt to assign
oApplicationusingsboGuiApi.GetApplication().To resolve this, ensure that the
SboGuiApiis correctly initialized and that the application is running in the expected context. Here’s a refined approach:SboGuiApi sboGuiApi = new SboGuiApi(); sboGuiApi.Connect(); // Ensure you connect to the GUI API first oApplication = sboGuiApi.GetApplication();Additionally, verify that your project references the correct SAP Business One SDK assemblies. If the issue persists, consider checking the compatibility of the SDK version with your application. Proper initialization and type checks can help prevent this casting error.
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 12, 2025, 1:11 AM EST
Hi Jayraj
I am making connection like this . How i can assign value to oApplication
I am able to connect. Now further what code i should write
SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company(); oCompany.Server = "[email protected]:30015"; oCompany.DbServerType = BoDataServerTypes.dst_HANADB; oCompany.CompanyDB = "_LIVE"; oCompany.UserName = "manager"; oCompany.Password = "123"; oCompany.language = BoSuppLangs.ln_English; oCompany.UseTrusted = true; int connectionResult = oCompany.Connect(); if (connectionResult != 0) { throw new Exception("Error connecting to SAP Business One: " + oCompany.GetLastErrorDescription()); }Thanks