Adding Report Viewers to Applications


Adding Report Viewers to Applications

After you have created a report, you can view it in a client application. Both desktop applications and Web applications can include a Crystal Reports viewer capable of loading and displaying a selected report.

Adding Reporting Capability to Desktop Applications

Any Windows application can display a report within a Windows form using the Crystal Reports Windows form viewer. In addition to displaying reports, the report viewer is also able to interact with other form controls that can dynamically update the report. Displaying a report in a Windows application requires the following steps: adding a report viewer to a form, binding a report to the report viewer, and interacting with other controls within the form.

Adding a Report Viewer to a Windows Form

To display a report in a desktop application, you must add a report viewer to the form. Begin by creating a Windows form named ReportViewer.cs as described in Chapter 8, "Developing Desktop Applications." Next , open the Visual Studio .NET Toolbox and drag the CrystalReportViewer control onto the form.

You can resize or move the report viewer around within the form and ideally anchor it to resize as the form is resized. The properties of the report viewer can initialize at design time or in response to user -driven events at runtime.

Before a report can display in the report viewer, however, you must bind a report object to the viewer. You can do this by assigning the ReportSource property of the report viewer to a path pointing to the .rpt file.

A report can bind at design time to initialize a report viewer or at runtime to display a variety of reports based on the user selection. The report viewer can also bind to a report that is available as a report Web service.

Interacting with Windows Forms Controls

The report viewer can interact with other controls within a Windows form with the help of the report object model exposed by the reporting engine. You can add code to a form's source file that responds to specific events and sets report viewer properties.

In the ReportViewer form, add two buttons: btn_Next and btn_Previous. These buttons will direct the report viewer to move to the next page or the previous page of the report. From the Visual Studio .NET form editor, select the Next button and choose the events view in the Properties window. Create an event handler for the Click event named btnNext_Click , as shown in Figure 10-22.

click to expand
Figure 10-22: Creating an event handler that links a button to the report viewer

Once you have created an event for a form object, the form's code view opens and points to the newly created event handler. Inside the event handler, the code is free to set the report viewer's properties or invoke its methods :

 private void ShowNextPage(object sender, System.EventArgs e) {     reportViewer.ShowNextPage(); } private void ShowPrevPage(object sender, System.EventArgs e) {     reportViewer.ShowPreviousPage(); } 

In this example, clicking the Next button advances the displayed report to the next page.

Adding Reporting Capability to Web Applications

Web applications can also display reports using the Crystal Reports Web form viewer. The Web form viewer can interact with other Web form controls and can dynamically update the report being displayed. Displaying a report in a Web application requires the following steps: adding a report viewer to a form, binding a report to the report viewer, and interacting with other controls within the form.

If an exception is immediately thrown and the Web form is unable to load the viewer, check to ensure that the ASP.NET system account has read-write access to the viewer files. Open the File Explorer and point to the following directory: \Program Files\Microsoft Visual Studio .NET\Viewers. Open the Folder Properties dialog box, click the Security tab, and click the Add button. Next, enter a new user reference as follows :

 <machine_name>\ASPNET 

Next, close the Folder Properties dialog box by clicking OK. Finally, set folder permissions for this user to Read, Write, and Modify. This should eliminate the exception and allow the report to display normally.

Adding a Report Viewer to a Web Form

To display a report in a Web application, you must add a report viewer to the form. Begin by creating a Web form named ReportViewer.aspx as described in Chapter 7, "Building Web Applications." Next, open the Visual Studio .NET Toolbox and drag the CrystalReportViewer icon onto the form. Visual Studio .NET adds the following registration code to the HTML view of the ReportViewer.aspx file:

 <%@ Register TagPrefix = "cr" Namespace="CrystalDecisions.Web"     Assembly="CrystalDecisions.Web" %> 

Later in the HTML, the following code designates the insertion point for the Crystal Reports viewer:

 <cr:CrystalReportViewer id=crystalViewer runat="SERVER"     Width="350px" Height="50px"> </crystal:CrystalReportViewer> 

Interacting with Web Forms Controls

The report viewer can interact with other controls within a Web form in the same manner as the Windows form. You can add code to a form's source file that responds to specific events and sets report viewer properties.

In the ReportViewer Web form, add two buttons: btn_Next and btn_Previous. These buttons will also direct the Web report viewer to move to the next page or the previous page of the report. From the Visual Studio .NET form editor, select the Next button and choose the events view in the Properties window to create an event handler for the Click event named btnPrevious_Click , as shown in Figure 10-23.

click to expand
Figure 10-23: Creating an event handler that links a button to the report viewer

Crystal Reports allows specific data values to be entered as parameters before generating reports. The Web form viewer also supports the hosting of such reports. You can design a Web application to retrieve user input through a Web form control and pass the input as a parameter to the report hosted by the Web form viewer using the ParameterFieldInfo property of the CrystalReportViewer object.

Improving Performance with Report Caching

Web form viewers have the added capability of caching reports on the server for more efficient retrieval. Because the caching function leverages the ASP.NET caches, only reports displayed in a Web form viewer can be cached, not reports displayed in a Windows form viewer. The Web form viewer uses caching when different users call the Uniform Resource Locator (URL) for the same dynamic page, such as http://localhost/MyProject/ReportViewer.aspx .

Report caching begins when you add a report to any Web project. For example, when you added the OpenIssuesReport.rpt file to the project, you automatically created an OpenIssuesReport.cs file in the project directory. The newly created OpenIssuesReport class is derived from the CrystalDecisions.Engine.ReportClass class. Table 10-1 describes the ReportClass methods and properties.

Table 10-1: Relevant ReportClass Methods and Properties

NAME

DESCRIPTION

CreateReport

Creates an instance of the strongly typed report

GetCustomizedCacheKey

Returns the default cache key to the ASP.NET cache

CacheTimeOut

Returns a timeout value for the ASP.NET cache, CachedReportConstants.DEFAULT_TIMEOUT, by default

IsCacheable

Returns a value indicating if the report is cacheable and is True by default

ShareDBLogonInfo

Indicates if database login should be shared and is False by default

You can create a cached report instance in code by invoking the object's default constructor. Because the OpenIssuesReport object is derived from ReportDocument, the resulting instance can bind to a Web form viewer to cache it:

 OpenIssuesReport cachedOpenIssuesReport = new OpenIssuesReport(); 

Then, you can use the cached report object as is or customize it to meet specific needs. Finally, you bind the cached report instance directly to the Web form viewer:

 reportViewer.ReportSource = cachedOpenIssuesReport; 

The Web form viewer control initializes the cached report instance with data and the current state of the report. On cache timeout, the cache is flushed and the Web form viewer control reinitializes the cache with the current report state. As with all cached pages interacting with a database, refreshing a cached report will result in additional database access. To limit unnecessary load on the database, the Web form viewer toolbar disables the Refresh icon by default.

Adding Reporting Capability to Web Services

You can also publish a report as a Web service. A report Web service can be accessed by any application operating on any platform that uses an appropriate viewer. When you add a report to a Web service project, Visual Studio .NET compiles the Web service into a DLL and generates an Extensible Markup Language (XML) file that describes the public functions, input parameters, data types, and return data types exposed by the Web service. The DLL and the XML files are both published to the Web server as a Web service. Any client can invoke the Web service via Hypertext Transfer Protocol (HTTP) and Simple Object AccessProtocol (SOAP) to pass the XML data to the Web service.

Once you have published the report Web service on a Web server, any Webenabled application can consume it on the client side. Users can drag the report Web service from the Visual Studio .NET Server Explorer into a desktop application or a Web application. A Web Services Description Language (WSDL) file is automatically generated on the client.

Creating a Reporting Web Service

Creating a report Web service begins with a new Web application or Web service project in Visual Studio .NET. First, add a new or existing report template to the project. In the case of the IssueTracker application, the OpenIssuesReport.rpt file will do. Second, select the report template in the Solution Explorer and open its pop-up menu. Select the Publish As Web Service menu item.

This creates a new file and adds it to the project with the same name as the report template, but with an .asmx file extension. This file is the source code for the newly created report Web service. You can easily modify the code to invoke another Web service or to perform additional security functions. Rebuild the solution to compile the Web service and make it available to clients . Once built, the Web service becomes available in the Server Explorer. You can also test it by entering the Web service URL into a Web browser, as shown in Figure 10-24.

click to expand
Figure 10-24: Testing the report Web service with Internet Explorer

Creating a Reporting Web Service Client

When the report Web service is published, its classes, objects, methods, properties, and return values are available in XML and therefore can be transmitted across the Web. Client applications can access this Web service and interact with the report and its data without needing anything other than the ability to read XML.

The report Web service client can be either a Windows application with a Windows form viewer or a Web application with a Web form viewer. In either case, the viewer displays the report by connecting to the report Web service for that report. The first step is to create a proxy class that abstracts the report's Web service. Begin by selecting Project ˜ Add Web Reference from the Visual Studio .NET menu. In the Add Web Reference dialog box, enter the URL that points to the location of the newly created report Web service, as shown in Figure 10-25.

click to expand
Figure 10-25: Adding a Web referece to a Visual Studio .NET project

After adding a new Web reference to the report Web service, Visual Studio adds a new entry for the Web service in the Solution Explorer under the Web References tree node. Visual Studio creates the Web service proxy object with which the C# code can interact. You can use the proxy object directly to load the report and assign it to the ReportSource property:

 IssueTracker.OpenIssuesReport reportService = new IssueTracker.OpenIssuesReport(); reportService.Load(); reportViewer.ReportSource = reportService; 

In this case, the code creates an object, calls a Load method, and sets the ReportSource property. The compiled code establishes an HTTP connection to the OpenIssuesReportService Web service and exchanges SOAP messages that contain XML-based information about the report.

In the Web service example, a single Web service abstracts a single report. However, server file reports represent a Web service gateway that serves as a single entry point for multiple reports. All server file reports share the same report Web service file, such as ServerFileReportService.asmx. You select the specific report to display by setting the ReportPath property of the ServerFileReport object. The ReportPath property specifies the relative path of the report on the server.




Developing. NET Enterprise Applications
Developing .NET Enterprise Applications
ISBN: 1590590465
EAN: 2147483647
Year: 2005
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net