Static classes and the Business Objects COM Library
-
26-09-2019 - |
Question
The following code below is from a winforms application that on a button event opens up an instance of business objects 6.5, refreshes the report and then dumps the data in the report into a csv file, and then quits the business objects instance.
The first time i run the code it works perfectly, however if i run it again i get an exception on the line
boApp.LoginAs(userName, Password, false, null);
The exception thrown is 'Invalid Object'.
I'm assuming this is down to the fact that boApp hasn't been re-initialised, and that it's my lack of knowledge regarding static classes that's the issue.
The calling method is this:
BO_Control.RefreshBusinessObjects(boReportsFolder, boExportsFolder, boReportName, exportFileName, startDate, endDate);
and this is the BO_Control class
static class BO_Control
{
static busobj.Application boApp = new busobj.Application();
static busobj.Document testDoc;
public static void RefreshBusinessObjects(string reportFolder, string exportFolder ,string boReportName, string exportFileName, string startDate, string endDate)
{
DateTime BoStart = DateTime.Now;
boApp.LoginAs(userName, Password, false, null);
boApp.Interactive = false;
boApp.Visible = false;
GetData(reportFolder, boReportName, startDate, endDate);
ExportData(exportFolder, exportFileName);
Console.WriteLine("BO_Export took {0} seconds.", DateTime.Now.Subtract(BoStart));
boApp.Quit();
}
static busobj.Document GetData(string reportFolder, string reportName, string startDate, string endDate)
{
Console.WriteLine(reportFolder + reportName);
testDoc = (busobj.Document)boApp.Documents.Open(reportFolder + reportName, true, false, null, null);
//Report Start Date
testDoc.Variables[1].Value = startDate;
//Report End Date
testDoc.Variables[2].Value = endDate;
//Area. Needs to be a semi-colon delimited string
testDoc.Variables[3].Value = "L;B;H;";
testDoc.Refresh();
return testDoc;
}
static void ExportData(string exportFolder, string exportFileName)
{
testDoc.Reports.get_Item(1).ExportAsText(exportFolder + exportFileName);
//2 = DoNotSaveChanges
testDoc.Close(2);
}
}
Solution
I moved the instantiation of the BOApp into the RefreshBusinessObjects Method and that seemed to do the trick
OTHER TIPS
i also use this login code, it works, but it need to click the OK button of BO login diaglog. is there some methods can skip this click button step?