Creating Simple Web Applications

While desktop applications are still in wide use today, a large percentage of new applications being developed are Web-based applications. Most of the reasons for this centers around maintenance and central management; it's much easier to maintain and manage an application that resides entirely on a single Web server than an application installed on thousands of business user machines across a company.

You'll notice that this scenario is titled Simple Web Applications, and the next is Advanced Web Applications. The reason for this separation is because of distinct requirements that Crystal Decisions has heard from its customers. This scenario, Simple Web Applications, is really just about getting reports up on the Web. The most common requirements here are HTML-only delivery of reports, basic parameter prompting, and database logon credentials.

Sometimes a Web page is nothing but a empty page with a Crystal Report on it. Other times, the report is integrated into an existing Web page with other HTML elements on it. Sometimes report parameters and database logon information are prompted for on the page: In other situations, one or both of these items are set transparently in the background. The possibilities are really only limited by your own imagination.

Because Crystal Reports is the world leader in reporting, it needs to work well in a variety of customer situations and platforms. For the desktop application space, most developers are using COM technology, but when moving to the Web, developers use a mix of COM (ASP), Java (JSP), and .NET (ASP.NET) technologies. Because of this, Crystal Reports 9 includes report viewers for each of these platforms.

Web Report Viewing in ASP

As you might know, COM is the underlying technology that runs Active Server Pages (ASP). Crystal Reports 9 has a report viewer built just for ASP (although it also works in any other COM-compliant Web platform such as Cold Fusion). This COM Report Viewer is conceptually similar to the ActiveX viewer in that it communicates with the report engine to display a report. However, instead of residing on the business user's machine like the ActiveX viewer, the COM Report Viewer resides on the Web server. When included in an ASP page, it renders any report to HTML. This means that although the business user still receives a rich view of the report, it comes to him in a zero-client environment; that is, no Crystal Decisions software or controls need be downloaded or installed to the business user's machine. This zero-maintenance report delivery architecture is one of the most popular uses of Crystal Reports today.

To use the COM Report Viewer to display a report in an ASP page, use the following code:

 <%  ' Create the COM Report Viewer Dim Viewer Set Viewer = CreateObject("CrystalReports.CrystalReportViewer") ' Set the COM viewer's report source to a path to an RPT file Viewer.ReportSource = "RAS://C:\MyReport.rpt" ' Call the ProcessHttpRequest method to write the HTML output of ' the report into the response stream Viewer.ProcessHttpRequest Request, Response, Session %> 

Figure 24.4 shows the result of this page.

Figure 24.4. The COM Report Viewer in action.

graphics/24fig04.jpg

Like the ActiveX viewer, the COM Report Viewer has a ReportSource property that is used to identify which report to display. You'll notice that in the case of the COM Report Viewer, a file path to an RPT file is set, prefixed with "RAS://". This prefix is used to indicate that the viewer should use the Report Application Server (RAS) to process the report. The ProcessHttpRequest method does the actual HTML rendering and writes the output of the report into the response stream. In the following example, there is no other HTML or ASP code in the page, but other code and HTML can coexist.

Like the ActiveX viewer, the COM Report Viewer has properties and methods that allow the business user to customize the viewer and the output it creates. Some of the common tasks, such as passing parameter field values and database logon information, can also be accomplished. The following code provides an example of this:

 <%  Dim Viewer Set Viewer = CreateObject("CrystalReports.CrystalReportViewer") Viewer.ReportSource = "RAS://C:\SecuredReportWithParam.rpt" ' Set the database logon credentials, in this case for the first ' table in the report, which then propagates across any ' subsequent tables in the report Viewer.DatabaseLogonInfos(0).UserName = "user id" Viewer.DatabaseLogonInfos(0).Password = "password" ' Set the value of the first parameter field to a string. Other ' data types such as numbers and boolean values can be set ' here as well. Viewer.ParameterFields(0).CurrentValues.Add "USA" %> 

To learn more about the specific properties and methods available, consult the COM Report Viewers help file installed with Crystal Reports 9, as well as the sample report wizard application that uses the COM Report Viewer.

Web Reporting in Java

Crystal Decisions has always had a huge market share in the Microsoft developer community. With Crystal Reports 9, it extends this market leadership to the Java community. For the first time, Java developers can building reporting into their Web applications with a 100% pure Java SDK.

As you've probably guessed, delivering reports on the web in Java is very similar to COM. Crystal Reports 9 comes with a server-side report viewer control written in Java which can be used inside JSP-based applications. It is functionally the same as the COM Report Viewer, but is a native Java component, which means that it is targeted toward use in J2EE applications. The following code listing illustrates a JSP page with a report hosted inside it:

 <%@ page import="com.crystaldecisions.report.web.viewer.*" session="true" %>  <% // create the viewer object CrystalReportViewer viewer = new CrystalReportViewer(); // set the report source to the path to an RPT file viewer.setReportSource("RAS://C:\\MyReport.rpt"); // call the processHttpRequest function to  render the report into // HTML, then write to the response stream viewer.processHttpRequest(request, response,                           getServletConfig().getServletContext(), null); %> 

As with the COM Report Viewer, the Java Report Viewer has a setReportSource function for identifying the report to display. Also similarly, the processHttpRequest function is called to render the report.

Like the other report viewers, common tasks such as passing database logon credentials and parameter field values can be accomplished via the Java Report Viewer. An example of this is seen in Listing 24.2.

Listing 24.2 Parametersand Logon with the Java Viewer

[View full width]

 <%@ page import="com.crystaldecisions.report.web.viewer.*, com.crystaldecisions.sdk.occa. graphics/ccc.gifreport.data.*"         session="true" %> <% // Create a viewer object and pass it a report CrystalReportViewer viewer = new CrystalReportViewer(); viewer.setReportSource("RAS://C:\\SecuredReportwithParam.rpt"); // Set the database logon credentials, in this case for the first // table in the report, which then propagates across any // subsequent tables in the report viewer.getDatabaseLogonInfos().getConnectionInfo(0).setUserName("user id"); viewer.getDatabaseLogonInfos().getConnectionInfo(0).setPassword("password"); // Set the value of the first parameter field Fields params = new Fields(); ParameterField param = new ParameterField(); Values paramValues = new Values(); ParameterFieldDiscreteValue paramValue = new ParameterFieldDiscreteValue(); params.add(param); paramValue.setValue(new String("1389")); paramValues.add(paramValue); param.setCurrentValues(paramValues); param.setName("Publisher"); viewer.setParameterFields(params); %> 

Web Reporting in .NET

Microsoft's new .NET technology, specifically ASP.NET, is becoming more and more popular each day. More developers are migrating their existing desktop or ASP applications to ASP.NET. Crystal Decisions was at the forefront of Web reporting in .NET. It began working with Microsoft years before the Visual Studio .NET product was released and co-developed Crystal Reports for Visual Studio .NET, which shipped with Visual Studio .NET itself. For more information on this product, see Appendix A. In Crystal Reports 9, Crystal Decisions has upgraded that product and integrated it back in to the mainstream Crystal Reports product.

The .NET Web Form Report Viewer works much the same way as the COM Report Viewer. It is a server-side ASP.NET Web control that resides on a Web server and is used inside aspx pages. It renders reports to HTML for zero-client Web report delivery.

The following is a snippet from an aspx page illustrating how to integrate a report into an ASP.NET page:

 <h1>My Report</h1>  <cr:CrystalReportViewer ReportSource="RAS://C:\\MyReport.rpt" /> 

You'll notice that unlike the COM and Java report viewers, you don't need to write code to display the report. Instead, you can use a special tag inside of the ASPX page to specify various property values. This is common to all .NET Web form controls such as the data grid and calendar control. The .NET Report Viewer has similar properties, events, and methods that allow for total control over how the report is delivered.

One of the nice things about adding reporting to an ASP.NET application is using the Visual Studio .NET development environment. Many of these properties can be set visually via the Property Browser. Figure 24.5 shows an ASP.NET Web form with the .NET Report Viewer hosted on it inside of the Visual Studio .NET development environment.

Figure 24.5. Adding reporting to an ASP.NET application.

graphics/24fig05.jpg

Like the other report viewers, the .NET Report Viewer can accept database logon credentials, parameter field values, selection formula, and so on. The following code listing provides an example of some of these actions in the C# language:

 // Some objects in the Shared namespace are needed  using CrystalDecisions.Shared; ... // Set the report source to something viewer.ReportSource = "C:\\Temp\\SecuredReportWithParam.rpt"; // Create a parameter value object, in this case a discrete value ParameterDiscreteValue paramValue = new ParameterDiscreteValue(); paramValue.Value = "USA"; // Add the value to the parameter's current values viewer.ParameterFieldInfo[0].CurrentValues.Add(paramValue); // Set the database logon credentials viewer.LogOnInfo[0].ConnectionInfo.UserID = "user id"; viewer.LogOnInfo[0].ConnectionInfo.Password = "password"; 

graphics/bookpencil_icon.gif

Both the Crystal Reports for Visual Studio .NET product and Crystal Reports 9 come with a report viewer based on the .NET Windows Forms framework. This report viewer is functionally similar to the ActiveX report viewer, but is a native .NET control. It can be used in either desktop applications (called Windows Applications in Visual Studio .NET), or on the client tier of ASP.NET applications.


For more information on both the .NET Windows Forms report viewer and the .NET Web Forms report viewer, consult the Crystal Reports 9 documentation installed into the MSDN help collection.



Sams Teach Yourself Crystal Reports 9 in 24 Hours
Sams Teach Yourself Crystal Reports 9 in 24 Hours
ISBN: B003D7JUVW
EAN: N/A
Year: 2005
Pages: 230

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