Now that you have built the report you can build a web page
to display it. I will create a simple page that lets the user enter a fiscal
year and view the report.
Right click on the Adventure Works web site in the Solution Explorer.
Select Add New Item… from the pop-up menu.
Select Web Form from the templates and change the name to
SalesReport.aspx. Make sure the Language is set to C# and click the Add button.
Open the page and view its 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.
Add the following code between the div tags.
<asp:TextBox runat="server" ID="txtFiscalYear"></asp:TextBox>
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click"
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
This adds a text box that will allow the user to enter the
fiscal year to print the report.
Add the following using statements in the code behind.
//Custom using statements
Add the following code the Page_Load event.
if (CrystalReportViewer1.Visible == true)
//Rebind the report.
Add the following code for the Preview button's click event. You can
create the event handler by double clicking on the button in Design mode.
protected void btnPreview_Click(object sender, EventArgs e)
CrystalReportViewer1.Visible = true;
This code calls a custom method called BindReport() and then
shows the Crystal Report Viewer Control.
Now add the following custom methods.
private void BindReport()
ReportDocument report = new ReportDocument();
report.DataDefinition.FormulaFields["FiscalYear"].Text = txtFiscalYear.Text;
CrystalReportViewer1.ReportSource = report;
private void SetTableLocation(Tables tables)
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = @"LTMTI30\SQL2008";
connectionInfo.DatabaseName = "AdventureWorks";
connectionInfo.UserID = "aspalliance";
connectionInfo.Password = "aspalliance";
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
tableLogOnInfo.ConnectionInfo = connectionInfo;
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 have created an "aspalliance"
SQL Login and have given it access to the database. The FiscalYear formula is
then set to the year the user typed into the report. You could add validation
to make sure the user entered a four digit year, but for simplicity I left this
out. The Crystal Report Viewer's source is then set to the report object.
You can now run the project. Set the SalesReport.aspx page
to the start page and run the project. Enter 2002 for the Fiscal Year and click
the Preview button.
You can scroll through the report using the navigation
buttons at the top of the page or you can click directly to a sales territory
by clicking the name in the group tree on the left hand side of the report.