Now that you have built the report you can build a web page
to display it. I'll create a simple page that lets the user click a button to
preview the report. We'll do all of this in this Default.aspx page that is
part of the web site we created.
1.
Open the Default.aspx page and view its HTML markup. Add the following
Register directive after the Page directive.
<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral,
PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR"%>
This allows you to use the Crystal Reports Viewer control
that comes with Visual Studio.
2.
Add the following code between the div tags.
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click"
Text="Preview" />
<br />
<br />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true" />
3.
Open the code behind page and add the following using statements in the
code behind.
//Custom using statements
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
4.
Add the following code to the Page_Load even handler.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Hide the crystal report viewer
CrystalReportViewer1.Visible = false;
}
else
{
//If this is a postback the rebind the report so the paging works.
if (CrystalReportViewer1.Visible == true)
{
//Rebind the report.
BindReport();
}
}
}
This will hide the Crystal Report Viewer Control when the
user initially navigates to the page. The postback check code is needed
because if the user is paging through the report a postback event is fired and
the report needs to be rebound to the Viewer control.
5.
Add the following code for the Preview button's click event.
protected void btnPreview_Click(object sender, EventArgs e)
{
BindReport();
CrystalReportViewer1.Visible = true;
}
6.
Now add the following custom methods.
public void BindReport()
{
ReportDocument report = new ReportDocument();
report.Load(Server.MapPath("SubReport.rpt"));
SetTableLocation(report.Database.Tables, "YourServer", "AdventureWorksLT");
SetTableLocation(report.Subreports[0].Database.Tables, "YourServer",
"AdventureWorks");
CrystalReportViewer1.ReportSource = report;
}
private void SetTableLocation(Tables tables, string server, string database)
{
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = server;
connectionInfo.DatabaseName = database;
connectionInfo.UserID = "aspalliance";
connectionInfo.Password = "aspalliance";
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
tableLogOnInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogOnInfo);
}
}
The first method creates an instance of the ReportDocument
class. This represents the report that you created earlier and allows you to
manipulate it at runtime. The SetTableLocation() method sets the table
location for each table in the report. Again, this assumes you've created an
"aspalliance" SQL Login and have given it access to the database.
The ReportDocument class exposes the sub report as a collection. You can have
more than one subreport in a report but for this example we only have one so we
have to set the location for the subreport with the index of 0. The Crystal
Report Viewer's source is then set to the report object.
You can now run the project by pressing F5. Click the
Preview button.