Let’s get to how to display our report. Assuming that we
have entered valid data in the parameter entries displaying in the panel, we
need to press the now visible “View Report” button. That button runs the code
to set the login parameters, bind the report to a Crystal Report Document, and then
bind that document to the Crystal Viewer on the page.
Once the button is clicked, the btnViewReport_Click event
fires. We instantiate a new CRDisplayReport object and then set the
reportname, dbname, servername, userid, and password property. Then we call
the method CreateReportToView, which returns a ReportDocument object that we
bind to the Crystal Report Viewer. See Code Listing 1 for the code.
Code Listing 1
CRDisplayReport oDisplay = new CRDisplayReport();
// This is where we put the selected reportname
and dbname etc. For the article this is hardcoded.
oDisplay.ReportName=@"C:\Dev\DNNCrystal\DesktopModules\CrystalReportManager\ReportLibrary\ListHits.rpt";
oDisplay.DBName="DNNReportManager";
oDisplay.ServerName="(local)";
oDisplay.UserID="DotNetNuke";
oDisplay.PassWord="DotNetNuke";
CrystalDecisions.CrystalReports.Engine.ReportDocument
oCRDoc = oDisplay.CreateReportToView(Request);
btnViewReport.Visible=false;
crvShowReport.Visible=true;
crvShowReport.ReportSource=oCRDoc;
You can see from this code that the Crystal Report Viewer is
bound to OCRDoc object. This object is created from the CreateReportToView
method. The code for this method is found in Code Listing 2.
Code Listing 2
CrystalDecisions.CrystalReports.Engine.ReportDocument
oReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
CrystalDecisions.Shared.ParameterDiscreteValue
oDiscreteParam = new CrystalDecisions.Shared.ParameterDiscreteValue();
CrystalDecisions.Shared.ParameterValues
oDefaultValues = new CrystalDecisions.Shared.ParameterValues();
try
{
oReport.Load(_reportName);
int iCurrParm = 1;
foreach
(CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition oCRParam in oReport.DataDefinition.ParameterFields)
{
oDiscreteParam.Value =
GetRequest(oCRParam.ParameterFieldName + iCurrParm.ToString(), oCRParam,
oRequest);
oDefaultValues.Add(oDiscreteParam);
oCRParam.ApplyCurrentValues(oDefaultValues);
iCurrParm+=1;
}
ApplyLoginInfo(oReport);
return oReport;
}
catch (Exception e)
{
throw new
Exception("CRDisplayReport.CreateReportView experienced an error: " +
e.Message);
}
This code, also modified from a previous article, creates a Reportdocument
oReport. We load the report using the _reportname property. iCurrParm follows
the convention used previously that makes sure that all parameter names are
unique. This is how you can find the parameter names created in Part 3 of this series.
We iterate through the all the parameters in the report
using a foreach loop. The value applied to each parameter (this assumes a
DiscreteParamValue), is applied via the GetRequest method.
The GetRequest method takes the parameter name, the
parameter object, and the request object to find the appropriate parameters.
Because we need to find the controls via the request rather than the
page.findcontrol method, we iterate through all the parameters in the request
to find the correct parameter passed.
Once that parameter is found through the request object, it
gets passed back to oDiscreteParam.Value. After the parameters get applied, we
need to pass the login information to the Report document. This is done
through the ApplyLoginInfo method shown in Code Listing 3.
Code Listing 3
CrystalDecisions.CrystalReports.Engine.Database
oCRDb = _oRpt.Database;
CrystalDecisions.CrystalReports.Engine.Tables
oCRTables = oCRDb.Tables;
CrystalDecisions.Shared.TableLogOnInfo
oCRTableLogonInfo;
CrystalDecisions.Shared.ConnectionInfo
oCRConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
oCRConnectionInfo.DatabaseName = _dbName;
oCRConnectionInfo.ServerName = _serverName;
oCRConnectionInfo.UserID = _userID;
oCRConnectionInfo.Password = _passWord;
foreach(CrystalDecisions.CrystalReports.Engine.Table
oCRTable in oCRTables)
{
oCRTableLogonInfo = oCRTable.LogOnInfo;
oCRTableLogonInfo.ConnectionInfo =
oCRConnectionInfo;
oCRTable.ApplyLogOnInfo(oCRTableLogonInfo);
}
This code simply applies the login information to the report
document. Once that’s done, the report object is returned back to the calling
program. In this case, that’s the User Control ReportsViewer
btnViewReport_Click event. Once the object is bound to the viewer, the
CrystalViewer objects Visible property is set to true. Now the report is
visible on the page!