Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: Publishing Crystal Reports using ASP.NET


Message #1 by Dinesh Chandrasekaran <princedinesh@y...> on Wed, 23 Oct 2002 03:50:32 -0700 (PDT)
How to publish crytal report using ASP.NET

Can any one of you tell the procedure in detail.

- Dinesh

 



---------------------------------
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
Message #2 by "Mike Gale" <info@d...> on Thu, 24 Oct 2002 09:28:13 +1300
Dinesh Chandrasekaran wrote:
> How to publish crystal report using ASP.NET
> 
> Can any one of you tell the procedure in detail.

I don't use Crystal Reports.

I see a lot of queries about how to make it work and adverse commentary.
If you can find a dedicated newsgroup that might be very useful.

Two comments since yesterday (I did not look for these they found me)

1)  You may remember that the Crystal API for Crystal 7 was known as
cr.api  
Maybe for a for good reason..

2)  Well... I solved the problem.
It appears that it is a bug in crystal reports that causes the record
selection formula to be ignored unless reset. 

Here is the rather silly code that fixes it:

string strFilter = crDocument.RecordSelectionFormula;
crDocument.RecordSelectionFormula = String.Empty;
crDocument.RecordSelectionFormula = strFilter;

Mike Gale, Decision Engineering (NZ) Ltd.


Message #3 by "Grosso, Gary J." <congjg@N...> on Thu, 24 Oct 2002 14:18:53 -0400
Crystal Reports Report Design Component

The following is taken from the Crystal Reports ASPRDC help file.

Primary Steps to using the Crystal Reports RDC through ASP
These steps outline and provide the basic order of operations that are
required to instantiate and utilize the Report Design Component COM objects.

Creating an Application Object
The Application object in the RDC's object library is the only object that
can be created. Using the Application object, you can obtain a report object
by opening a report file, manipulate aspects of the report object, such as
select formulas and sort fields, then view or export the report. Since the
Application object is the only creatable object exposed by the RDC, you must
create an Application object before you can perform any other tasks using
the Design Component. In order to create the Application object in your ASP
application use code similar to the following:
 
If Not IsObject(session("oApp")) Then
  Set session("oApp") = Server.CreateObject("CrystalRuntime.Application")
End If
 
The  If...End If   structure is used to create the RDC Application object
only once per session.  Creating the application object (session ("oApp"))
loads the Crystal Reports Designer Component automation server (Craxdrt.dll)
into memory.
 
We create it as a session variable in order to use it for the duration of
the ASP session.  This is to eliminate the overhead of loading and unloading
the Craxdrt.dll in to and out of memory.  Once the application object is
created in memory for this session, you can run many reports without having
to recreate it.
 
 
Obtaining a Report Object
You obtain a Report object by specifying a Seagate Crystal Reports (.RPT)
file and opening it with the OpenReport method of the Application object:
 
If IsObject(session("oRpt")) then
  Set session ("oRpt") = session
("oApp").OpenReport("c:\reports\xtreme.rpt", 1)
End If
 
The OpenReport method has only two parameters; the path of the report file
you want to access and the OpenMethod. (The OpenMethod is set to one so that
the report is not locked in an open exclusive mode.) By setting a report
object according to the return value of this method, you can proceed to
manipulate, preview, or export the report using other objects, methods, and
properties available in the Crystal Report Engine Automation Server's object
library.
 
Notice that we do not create the report object only once.  This is because
within an ASP session, you may want to process more that one report.  The
Rptserver.asp component will only process a report object named session
("oRpt").  Therefore, if you with to process more than one report in an ASP
session, you must open that report by creating a new session ("oRpt")
object.
 
** Note:  It is also possible to create a handle to a report object using
the Application Objects NewReport method.  This method will create a blank
report to which we will need to add a data source, fields, formatting etc. 
Using the Report Object

Once you obtain a Report object, you can use that object to make runtime
changes to the report file, and then send the report to a viewer, export it
to various alternate formats, save it to a disk file or send it to an e-mail
address. Note that the changes you make at runtime are not permanent; they
do not change the original report file, they only affect the output of the
report during the current Crystal Report Engine session.  Through the report
object, you obtain access to different aspects of the report file, such as
selection formulas, sub reports, sort fields, and format settings. For
example, the following code changes the record selection formula for the
report:
 
session("oRpt").RecordSelectionFormula = "{customer.Region} = 'CA'"
 
Once you make all desired changes and review settings for the report using
the functionality available in the automation server, you can preview or
export the report just as you do from Crystal Reports.  For more information
about methods for the Report object, search for each method by name in
Developer's online Help. (Crrdc.hlp)
 
 
Obtaining a PageEngine Object
The PageEngine generates pages of a report on the web server and sends the
pages to the client web browsers as they are requested.  For example, when a
user first requests a report, only the first page is sent to the web
browser.  If the user pages forward or backward in the report, or requests a
specific page, only that page is sent.  This limits the resources required
by the web server and reduces download time for the client browser.  The
PageEngine is references through the Report object.
 
	If IsObject(Session("oPageEngine")) Then
		Set session("oPageEngine") = nothing
	End If
	Set session("oPageEngine") = session("oRpt").PageEngine
 
Again notice that we create the PageEngine object more that once.  The
Rptserver.asp component requires the session("oPageEngine") object to be
present so we reference a new PageEngine object for every new report object.
 
 
Referencing a Crystal Smart Viewer
Once we have the Application, Report and PageEngine objects in place, and
have manipulated the Report object as we desire, we can then reference on of
the Crystal Smart Viewers.  There are several different Smart Viewers, each
having its specific features and strengths.  The available viewers are:
	
	Smart Viewer for ActiveX
	Smart Viewer for Java using the Browser JVM
	Smart Viewer for Java using the Java Plug-in
	Smart Viewer for Netscape Plug-in
	Smart Viewer for HTML with Frames
	Smart Viewer for Standard HTML 

The CMDM Reporting Project uses the Smart Viewer for ActiveX

An Overview of an ASP Sample
Here is a break down of the Simple Preview Report example.  This example
contains all the basic elements that are required to bring up a report over
the web through ASP.
 
reportname = "SimplePreviewReport.rpt"
 
In our samples you will see the report name set as the variable,
"reportname".  This will allow the page to be modified easily if the report
name changes.  This report name can also be requested from an HTML form
using the Request object of ASP.  
 
 
If Not IsObject ( session ("oApp")) Then                              
Set session ("oApp") = Server.CreateObject("CrystalRuntime.Application")
End If
 
These lines create the application object.  The object must be a session or
application scoped object and must be called oApp, as this is the name that
the Rptserver.asp (more about this file later) is expecting.  You will
notice that the code is using if Not IsObject.  This will force the
application object to be cached for the session.  This object can be used
over and over again to bring up reports.  
 
 
The next thing that occurs is the local path to the report is resolved.
 
Path = Request.ServerVariables("PATH_TRANSLATED")
While (Right(Path, 1) <> "\" And Len(Path) <> 0)
iLen = Len(Path) - 1
Path = Left(Path, iLen)
WEND
 
This code uses built in Server Variables Collection of ASP and builds the
path to the report.  The path is the local path on the web server. 
 
The next step that occurs is the report is opened and the report object is
created.
 
If isObject(session("oRpt")) then
	set session("oRpt") = nothing
End If
 
On Error Resume Next
 
Set session("oRpt") = session("oApp").OpenReport(Path & ReportName, 1)
 
If Err.Number <> 0 Then
	Response.Write "Error occurred creating the report object: "
Err.Description
	Set Session("oRpt") = nothing
	Set Session("oApp") = nothing
	Response.End
End If
 
This code first will check to see if there is already an oRpt object in the
session.  If there is then it will destroy it and then create the object.
The object must be a session object and called oRpt. The report is opened
with the OpenReport method of the application object.  It is important to
pass a "1" after the ReportName as this will open up a copy of the report
and not the actual report.  If the ",1" is omitted an access denied error
may occur when trying to open the report.  It is also important that we
check for errors on the instantiation of the report object so that if there
is any difficulty in creating this object, we clean up the Application
object and free the license being used by that object.
 
session("oRpt").MorePrintEngineErrorMessages = False
session("oRpt").EnableParameterPrompting = False
 
These lines disable the Error reporting mechanism included the built into
the Crystal Report Design Component automation server (craxdrt32.dll).  This
is done for two reasons:
 
1.  The print engine is executed on the Web Server, so any error messages
    will be displayed there.  If an error is reported on the web server, the
    print engine will stop processing and you application will "hang".
 
2.  This ASP page and Rptserver.asp have some error handling logic designed
    to trap any non-fatal errors (such as failed database connectivity) and
    display them to the client browser.
 
**IMPORTANT**  Even though we disable the extended error messaging of the
engine, fatal errors can cause an error dialog to be displayed on the Web
Server computer.  For this reason we recommend that you enable the "Allow
Service to Interact with Desktop" option on the "World Wide Web Publishing"
service (Internet Information Server service).  That way if your ASP
application stops responding you will be able to view the error dialog (if
one is displayed).  The next part of the code accesses the database.
 
On Error Resume Next
 
session("oRpt").ReadRecords
 
If Err.Number <> 0 Then                                               
Response.Write "Error Occurred Reading Records: " & Err.Description
  	Set Session("oRpt") = nothing
  	Set Session("oApp") = nothing
  	Response.End
Else
 	If IsObject(session("oPageEngine")) Then

  		set session("oPageEngine") = nothing
  	End If
  	set session("oPageEngine") = session("oRpt").PageEngine
End If
 
The line of code containing "ReadRecords" is where the database is accessed
and the records are read into the report.  This is where many Crystal
Reports ASP applications will stop processing.  This can be due to not
logging onto the database in the script or passing invalid parameters.  It
is important to trap for any errors that may occur so that the application
and report objects can be destroyed in order to free up the license being
used by the application object.
 
The next section of code creates the Page Engine.  This creates the pages on
the server that are sent to the client's browser.  The object must be a
session object and called "oPageEngine".  There is a check to see if the
Page Engine object exists in the session.  If the object exists then it is
destroyed and the object is created.  
 
The next portion of the code brings up the viewer (in this case the Report
Viewer for ActiveX) and displays the object.  This is just an include file
to determine which browser to use.
 
<!-- #include file="SmartViewerActiveX.asp" -->
 
 
This overview provides the basic essential components to viewing a report
through the use of the Report Design Component and ASP.  It is important to
note that this example assumes the report is based of an unsecured data
source and does not take any parameters or have any sub-reports. 

About the Rptserver.asp
This section provides a brief overview of the prewritten page -
Rptserver.asp.    This page (which Crystal Reports provides with our
examples) is a prewritten page meant for use in Active Server Page
applications.  This page operates as a server side component to mediate the
communication between the report viewers on the client and the report object
on the server.  The Rptserver.asp handles viewer requests by parsing out the
commands, executing or handing off the action to the necessary objects, and
returning the result back to the client viewer.
 
This file should never be modified.  The code is written to exist as a
server side component and technical support will not support changes or
modifications to this file.

  Return to Index