Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

14.03 Base Windows Code Templates

Listing 14-1. Windows client, embedded report, Crystal viewer
[VB.NET]
Dim myReport As New CrystalReport1
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalReport1 myReport = new CrystalReport1;
CrystalReportViewer1.ReportSource = myReport;

In this code, the following assumptions are made: there is an embedded report called CrystalReport1, and that a viewer control has been added to the current form. The report was also created within the Windows project and automatically added to the Solution Explorer as a class.

The first line of code instantiates the class and assigns it to the myReport object variable. The second line of code assigns this object to the ReportSource property of the viewer control. This automatically shows the user a preview of the report within the viewer.

Listing 14-2. Windows client, non-embedded report, Crystal viewer
[VB.NET]
Dim myReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
myReport.Load("CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = myReport
[C#]
CrystalDecisions.CrystalReports.Engine.ReportDocument() myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myReport.Load("CrystalReport1.rpt");
CrystalReportViewer1.ReportSource = myReport;

In this code, it is assumed that there is a non-embedded report called “CrystalReports1.rpt” that is saved to the same directory where the project’s executable is located. There is also a viewer control that has been added to the current form. The first line of code declares and instantiates the ReportDocument class and assigns it to the myReport object variable. The second line of code uses the Load() method to load the report from the hard drive into memory. The last line of code assigns the object to the ReportSource property of the viewer control. This automatically shows the user a preview of the report within the viewer.

Non-embedded reports can be a little confusing when you first start working with them because they are not located in the same path as your application’s executable. Visual Studio compiles your executable into a sub-folder named \Bin\Debug (or \Bin\Production depending upon how you compile it), but your report file is located with the source code. Thus, it is two sub-folders back and the code in Listing 14-2 won’t work. To make this work, you either have to copy the report file to the Debug folder or modify your code so that it references the subfolder. If you choose to do that, you would use the code in listing 14-3.

Listing 14-3. Windows client, non-embedded report, Crystal viewer
[VB.NET]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yBase.Load
CrystalReportViewer1.ReportSource = "..\..\CrystalReport1.rpt"
End Sub
[C#]
private void Form1_LOAD(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = @"..\..\CrystalReport1.rpt";
}