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.
Open the Default.aspx page and view its markup. Add the following
Register directive after the Page directive.
<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0,
This allows you to use the Crystal Reports Viewer control
that comes with Visual Studio.
Add the following code between the div tags.
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click" Text="Preview" />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
Add the following using statements in the code behind.
//Custom using statements
Add the following code for the Preview button's click event.
protected void btnPreview_Click(object sender, EventArgs e)
ViewState["ParametersShown"] = "True";
This code calls a custom method called BindReport() and then
stores a value of true to persist the fact that the parameters have been shown
to the user. Since we are using the Crystal Viewer Control it will
automatically display a page to collect the values for the parameters in the
Now add the following custom methods.
private void BindReport()
ReportDocument report = new ReportDocument();
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've created an
"aspalliance" SQL Login and have given it access to the database.
The Crystal Report Viewer's source is then set to the report object.
Add the following to the Page_Load event.
//Check if the parmeters have been shown.
if ((ViewState["ParametersShown"] != null) &&
(ViewState["ParametersShown"].ToString() == "True"))
This will call the BindReport method again once the user has
entered the parameters. Crystal keeps these values on postback.
You can now run the project by pressing F5. Click the
Preview button. You should be prompted for the Sales Forecast Factor, the Start
Date, and the End Date.
Enter 5 for the Sales Forecast Factor. This will increase
the sales numbers by 5%. Click on the icon next to each date to set the start date
to 1/1/2002 and the end date to 12/31/2002. Now click the Submit button.
The first page of the report should show the original sales
numbers and the second page will show the forecast sales numbers.