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 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.
Add the following code between the div tags.
<asp:Button ID="btnPreview" runat="server" onclick="btnPreview_Click"
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
Open the code behind page and add the following using statements in the
//Custom using statements
Add the following code to the Page_Load even handler.
protected void Page_Load(object sender, EventArgs e)
//Hide the crystal report viewer
CrystalReportViewer1.Visible = false;
//If this is a postback the rebind the report so the paging works.
if (CrystalReportViewer1.Visible == true)
//Rebind the report.
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.
Add the following code for the Preview button's click event.
protected void btnPreview_Click(object sender, EventArgs e)
CrystalReportViewer1.Visible = true;
Now add the following custom methods.
public void BindReport()
ReportDocument report = new ReportDocument();
SetTableLocation(report.Database.Tables, "YourServer", "AdventureWorksLT");
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;
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