Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

18.02 Consuming Web Services

The second part is creating an application to consume the 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).

Right-click on the project and select Add Web Reference. You are prompted to enter the location of where to look for the web service.



Figure 18-5. Browse the existing web services.

Click on the option “Web Services on the local machine”.

This shows you a list of the available web services that have been installed on your computer. Select the web service that you just created.

Click the Add Reference button to close the dialog box and create the web reference in your project.

Add a CrystalReportViewer control to your form and bind it to the web service. The following code performs the binding.

Listing 18-1. Consuming a project’s web service
Private Sub Form1_Load(ByVal sender As
CrystalReportViewer1.ReportSource = New localhost.EmployeeListService
End Sub

This code creates a new instance of the EmployeeListService class and passes it to the ReportSource property of the viewer. Of course, you will want to replace the EmployeeListService name with the name of the report class being used in your application.

Run the application and the viewer will display the report.

You can bind to a report web service using only the URL. This lets you consume any report web service without having to add it to your project.

Listing 18-2. Preview a report using the web service’s URL.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = "http://localhost/VBWebService/EmployeeListService.asmx"
End Sub

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 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 then assign the ReportSource property to Nothing.

Listing 18-3. Print a web service report without previewing it.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource =
"http://localhost/VBWebService/EmployeeListService.asmx"
CrystalReportViewer1.PrintReport()
CrystalReportViewer1.ReportSource = Nothing
End Sub