Crystal solution for printing reports in the Web Environment

Source: Internet
Author: User
Tags mdb database
Http://www.sqlsky.com/asp/070730/10271/

Http://www.dmtzj.com/shownews.asp? Newsid= 240

<% @ Language = "VBScript" %>
<%
'========================================
'Generate a report directly from ADO recordset
'========================================
'

'Concept:
'
'The application is designed to demonstrate how to generate a report from ADO recordset. We first establish the ADO connection and
'Recordset object, and then generate a record set from the database using SQL statements. Then we create a crystal
'Reports object, and point this object to ADO recordset. Finally, we set Crystal Reports
'Smart viewer sends the report to the client.

 

'Step 1: Create An ADO connection and recordset

'An ADO database connection is accessed through an existing ODBC Data Source (DSN) from an application such as ASP.
'Data connection. To achieve the purpose of this example, we will use
'Connect to the ACCESS database Xtreme. mdb system DSN

'Create an ADO database connection:

Set oconn = server. Createobject ("ADODB. Connection ")

'Create an ADO connection called "oconn" here. We will use this ADO connection object to connect to the DSN

'To use ADO connection, you must first open it:

Oconn. Open ("Xtreme Sample Database ")

'Open the ODBC Data source. This data source points to the ACCESS database Xtreme. MDB.

'Now we must create a recordset object:

Set SESSION ("ors") = server. Createobject ("ADODB. recordset ")

'The above we created a session ("ors"). This session stores A recordset object.
'Contains the data returned by SQL statements.

'Define and generate recordset:

Session ("ors"). activeconnection = oconn
'Define the connection object to be used by this recordset.

Session ("ors"). Open "select [product ID], [product name] from product"

'Use an SQL statement to retrieve two fields from the "product" table of the Xtreme. mdb database

'================================================ ========================================================== ====
'Create a Crystal Reports object
'================================================ ========================================================== ====
'You may have noticed that the Crystal Reports object is set as session, because it is called
To enable rptserver. asp to easily access the Crystal Report object,
'Let's set these objects to sessions. In this way, any ASP page runs in this session and can directly access these objects.

Reportname = "adorecordset. rpt"

'Create a string variable to point to the crystal report file (. rpt file ).CodeTime
'Change to your crystal report file name.

'Create Application Object
If not isobject (Session ("oapp") then
Set SESSION ("oapp") = server. Createobject ("crystalruntime. application ")
End if

'This "If/end if" structure is used to create only one Crystal Reports application object o for each session
'Create Application Object-session ("oapp"), and set the crystal report design component
'Automation server (craxdrt. dll) is loaded into the memory.
'
'The session variables are created to use them in the ASP session process. This reduces the number of craxdrt. dll
'System overhead for loading and detaching. Once an application object is created in a session, we do not need
'Recreate the object to run more reports.

'Create a report object
'
'The report object is created by the openreport method of the application.

Path = request. servervariables ("path_translated ")
While (right (path, 1) <> "" And Len (PATH) <> 0)
Ilen = Len (PATH)-1
Path = left (path, ilen)
Wend
Response. Write path
The 'while/wend' loop is used to convert the current file from the virtual path (eg: http: // domain/DIR) to the Crystal
'Report file physical path (eg: C :)

'Open the report (clear any previous objects first)

If isobject (Session ("orpt") then
Set SESSION ("orpt") = nothing
End if

On Error resume next

Set SESSION ("orpt") = SESSION ("oapp"). openreport (Path & reportname, 1)
'Here, the "path" and "reportname" variables are used to calculate the physical path of the crystal report file and open it.

If err. Number <> 0 then
Response. Write "error occurred creating report object:" & err. Description
Set SESSION ("orpt") = nothing
Set SESSION ("oapp") = nothing
Session. Abandon
Response. End
End if

'This on erro resume next block checks any errors that occur when a report object is created. We are explicitly capturing any
'Error if the view exceeds the maximum number of concurrent users specified by the license agreement.

'Note: we do not just create a report object once. This is because with ASP session, you can process more than one report.
'Rptserver. asp will only process a report object called SESSION ("orpt. Therefore, if you want to process multiple reports
Create a new session ("orpt") object.

Session ("orpt"). moreprintengineerrormessages = false
Session ("orpt"). enableparameterprompting = false

'The error reporting mechanism is not allowed here, including the crystal report design Component Automation server (craxdrt. dll)
'Built-in error reporting for two reasons:
'1. The printing engine is executed on the Web server, so any error information will be displayed on the server. If an error is reported on the server,
'Print engine will stop running and your application will be "suspended"
'2. rptserver. asp already has some error processing logic in it, which can capture any non-fatal error and display it on the client.
'
'** Important **: even if the server engine error handling is disabled, the fatal error will still be captured on the Web server and
'The error prompt dialog box is displayed. Therefore, we recommend that you set
'"Allow service to interact with desktop" option. In this way, if your ASP application is dead, you will be able to see the error message.

'================================================ ========================================================== ========
'================================================ ========================================================== ========

'Now we must tell the report data in ADO recordset.

'Report is based on dynamic ADO recordset. We must establish a report based on our recordset.
'Then we will tell the report data in the ADO record set at runtime. Reports usually rely on database structure files
'(Adorecordset. TTX) is created. This. TTX file contains the recordset structure and does not contain actual data.

'A crystal report fully depends on the data structure of the report to be used, so your database structure file (TTX file) at runtime)
'Or it is very important to reflect the DSN of the data contained in the ADO recordset.

Session ("orpt"). discardsaveddata
Set database = SESSION ("orpt"). Database
'The database used to instantiate the report

Set tables = database. Tables
'Instantiate the table in the database object

Set Table1 = tables. Item (1)
'Instantiate the first table. In this instance, this table object directs to the adorecordset. TTX file.

Table1.setprivatedata 3, Session ("ors ")

'"Setprivatedata" tells the report that the current data source is recordset, and the data to be displayed in the report is included in the Session ("ors ").
'If your report contains a subreport, different recordsets will be provided to point to the data of the subreport.
'
'================================================ ========================================================== =====
'Get record again and create "page on demand" engine object
'================================================ ========================================================== =====

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
Session. Abandon
Response. End
Else
If isobject (Session ("opageengine") then
Set SESSION ("opageengine") = nothing
End if
Set SESSION ("opageengine") = SESSION ("orpt"). pageengine
End if

'Instantiate Crystal Reports smart Viewer
'
'Use the Crystal Reports Automation server in the ASP environment. We use the same page to call it through the Crystal Web Report Server.
'"Smart viewers"
'There are four Crystal Reports smart viewers:
'
'1. ActiveX smart Viewer
'2. Java smart Viewer
'3. html frame smart Viewer
'4. html page smart Viewer
'
'The smart viewer you use will be compatible with the browser you use. For example, you will not use Java viewer If your browser
'Java applets is not supported. For this reason, in this demo, we have selected to define a viewer. You can decide through the Code
'Compatibility of the requested browser. In any case, this function inherits from the Crystal Reports Automation server,
'Exceeds the scope of this example.

'For simple reasons, we have chosen to implement this function through the functions included in the ASP server. You can choose different
The 'smartviewer *. asp file is sent to different browsers and simply replaced by the smart viewer ASP file you want.

'These options are: smartvieweractivex. asp, smartviewerjave. asp, smartviewerhtmlframe. asp,
'And smartviewerhtmlpage. asp. Note that when using these include files, you must place the corresponding. asp files in the same master
'Asp files are in the same virtual path.
'
'* Note * For smartviewerhtmlframe and smartviewerhtmlpage, you must have framepage. asp in the virtual path.
'File and toolbar. asp file

Viewer = request. Form ("Viewer ")

'Read the value of the used Viewer and put it in the variable "Viewer ".

If CSTR (viewer) = "ActiveX" then
%>

<%
elseif CSTR (viewer) =" Netscape plug-in "then
%>

<%
elseif CSTR (viewer) =" Java using browser JVM "then
%>

<%
elseif CSTR (viewer) =" Java using Java Plug-in "then
%>

<%
elseif CSTR (viewer) =" HTML frame "then
response. redirect ("htmstart. ASP ")
else
response. redirect ("rptserver. ASP ")
end if
'the above if/then/else is designed to test the value of the" Viewer "variable. Based on this value, send appropriate crystal smart viewer
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.