Introduction
This article will help you to learn how to bind a Crystal Reports file with the Crystal Reports Viewer and display it in a Web Page using very simple steps.
Using the code
The code is very simple. Follow the steps below or download the attached file. Create a report in your project under the Reports folder to try this out.
- Reference these namespaces:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
- Declare a private report object in your main class:
private ReportDocument reportDocument;
- Implement the
Page_Init
method in your web page with the following code:
private void Page_Init(object sender, EventArgs e)
{
reportDocument = (ReportDocument)Session["reportDocument"];
crystalReportViewer.ReportSource = reportDocument;
}
- The method below will set the default logon information to the
CrystalReportViewer
:
public void setLogonInfo()
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
logOnInfo.ConnectionInfo.ServerName = "srvr";
logOnInfo.ConnectionInfo.DatabaseName = "database";
logOnInfo.ConnectionInfo.UserID = "sa";
logOnInfo.ConnectionInfo.Password = "pwd";
TableLogOnInfos infos = new TableLogOnInfos();
infos.Add(logOnInfo);
crystalReportViewer.LogOnInfo = infos;
}
- Implement the code below for the button click event:
protected void display_Click(object sender, EventArgs e)
{
reportDocument = new ReportDocument();
reportDocument.Load("C:\\myExample\\Reports\\OnlineTest.rpt");
Session["reportDocument"] = reportDocument;
crystalReportViewer.ReportSource = reportDocument;
setLogonInfo();
}
[Optional step]
- The code below can be used to retrieve conditional data through the
CrystalReportViewer
. This can be written wherever you want to implement it. Let's say, by clicking on Search By Employee Id, you want to display some employee data in the ReportViewer
and print it:
crystalReportViewer.SelectionFormula =
"{Table.FieldName}=" + DynamicValue + "";
That's all!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.