Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

18.02 Consuming the Report Service

Consuming the Report Service

The second part is creating an application to consume the report’s web service. To create an application that consumes the web service, open a new instance of the Visual Studio IDE and create a new Windows application (or an ASP.NET application).

Add a CrystalReportViewer control to your form and bind it to the web service. This is done by assigning the web service URL to the viewer’s ReportSource property. The following code performs the binding.

Listing 18-1. Consuming a project’s web service
[VB.NET]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
MyBase.Load
CrystalReportViewer1.ReportSource = "http://localhost/WebSite/EmployeeListService.asmx"
End Sub
[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
crystalReportViewer1.ReportSource = "http://localhost/WebSite/EmployeeListService.asmx";
}

Notice that when setting the ReportSource property with a URL, the viewer doesn’t need to have any special properties set. In other words, a viewer can display any web service report as long as it’s passed the URL. This lets you create an application that is a generic report viewer and display a number of reports. You could have one web service which returns a list of all the reports on the server and an application which retrieves this list and previews whichever one the user chooses.

Web services can only be consumed by the viewer control. Passing the web service URL to a ReportDocument results in an error. The only time you really need to use a ReportDocument object is when you want to print the report without previewing it first. To get around this problem, load the report into the viewer, call the PrintReport() method, and assign the ReportSource property to Nothing.

Listing 18-2. Print a web service report without previewing it.
[VB.NET]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
CrystalReportViewer1.ReportSource = "http://localhost/WebSite/EmployeeListService.asmx"
CrystalReportViewer1.PrintReport()
CrystalReportViewer1.ReportSource = Nothing
End Sub
[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
crystalReportViewer1.ReportSource ="http://localhost/WebSite/EmployeeListService.asmx";
crystalReportViewer1.PrintReport();
crystalReportViewer1.ReportSource = null;
}