I'm using Visual Studio 2013 and I'm trying to open reports created by Crystal Reports XI. Where I work, there are about 900 reports that other members of the dev team need to be able to open (they don't have CR installed on their machines) and I've been asked to create a utility that can open a report and show the SQL query that created the report. I've inserted a CrystalReportViewer control on my winform and use the Crystal Reports SDK to load a report into it and extract the query.
When the report opens, a window for editing the parameters for the main report opens, but not for the subreport. As a result, the main report opens in the viewer, and I can get the SQL for it and I get an error message telling me parameters are missing for the subreports. How do I bring up the parameter window for the subreports?
Below is the code I use to load the reports, the error occurs at getSQL. I call login and getSql from a button click handler:
privateReportDocument cryRpt, crySubRpt;//report and subreport documents
privateTableLogOnInfos crTableLogonInfos;
privateTableLogOnInfo crTableLogonInfo;
privateConnectionInfo crConnectionInfo;
privateSections crSections;
privateTablesCrTables;
privateReportObjects crReportObjects;
privateSubreportObject crSubreportObject;
privatevoid login()
{
crConnectionInfo.ServerName="SQLANYWHERE16";
loginMainReport();
loginSubReport();
popoulateReportViewer();
}//end void login
privatevoid loginMainReport()
{
CrTables= cryRpt.Database.Tables;
foreach(CrystalDecisions.CrystalReports.Engine.Table aTable inCrTables)
{
crTableLogonInfo = aTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo= crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogonInfo);
}
}//end loginmainreport
privatevoid loginSubReport()
{
// set the sections object to the current report's section
crSections = cryRpt.ReportDefinition.Sections;
// loop through all the sections to find all the report objects
foreach(Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects in there to find all subreports
foreach(ReportObject crReportObject in crReportObjects)
{
if(crReportObject.Kind==ReportObjectKind.SubreportObject)
{
crSubreportObject =(SubreportObject)crReportObject;
//open the subreport object and logon as for the general report
crySubRpt = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
foreach(CrystalDecisions.CrystalReports.Engine.Table aTable inCrTables)
{
crTableLogonInfo = aTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo= crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogonInfo);
}//end foreach table atable
}//end if crReportObjectKind
}//end foreach reportobject
}//end foreach section
}//end loginsubreport
privatevoid populateReportViewer()
{
crystalReportViewer1.ReportSource= cryRpt;
crystalReportViewer1.Refresh();
}//end void populateReportViewer
privatevoid getSQL()
{
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument boReportClientDocument;
CrystalDecisions.ReportAppServer.Controllers.RowsetController boRowsetController;
string temp ="";
string mainSqlQuery ="";
string subSqlQuery ="";
try
{
// Access the ReportClientDocument in the ReportDocument boReportClientDocument = cryRpt.ReportClientDocument;
//Get the Rowset controller to access the sql query boRowsetController = boReportClientDocument.RowsetController;
mainSqlQuery = boRowsetController.GetSQLStatement(newCrystalDecisions.ReportAppServer.DataDefModel.GroupPath(),out temp);
//output the query
outputToTextBox("-- main report sql query: "+Environment.NewLine+ mainSqlQuery +Environment.NewLine);
// get subreport sql
foreach(string subReportName in boReportClientDocument.SubreportController.GetSubreportNames())
{
//open the subreport
SubreportClientDocument boSubReportClientDocument = boReportClientDocument.SubreportController.GetSubreport(subReportName);
//Get the Rowset controller to access the subs sql query
RowsetController boSubRowsetController = boSubReportClientDocument.RowsetController;
subSqlQuery = boSubRowsetController.GetSQLStatement(newCrystalDecisions.ReportAppServer.DataDefModel.GroupPath(),out temp);
//output the subreport query
outputToTextBox("-- subreport: "+ subReportName +Environment.NewLine+"-- report sql query: "+Environment.NewLine+ subSqlQuery +Environment.NewLine);
}
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}//end void getSQL