Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP & Crystal... need serious help, I have no idea

Status
Not open for further replies.

Zooo

Programmer
Nov 7, 2000
19
0
0
CA
This one a mutiple parts.
A little background.
I am beginner/intermediate user of CR and a newbie to ASP. I have a report created that is already connected to an SQL database. It works fine.

I know that you can use ASP to connect to a report. I have been searching the forums looking for a way to do that but none of the examples I have come across seem to work for me. So help doing that would be great. :)

Also...
1. Can you use ASP to connect a report to different databases at runtime.
2. If so how?

Any help is greatly appeciated.
 
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) <> &quot;\&quot; 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(&quot;oRpt&quot;)) then

set session(&quot;oRpt&quot;) = nothing

End If



On Error Resume Next



Set session(&quot;oRpt&quot;) = session(&quot;oApp&quot;).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(&quot;oRpt&quot;).MorePrintEngineErrorMessages = False

session(&quot;oRpt&quot;).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 &quot;hang&quot;.



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 &quot;Allow Service to Interact with Desktop&quot; option on the &quot;World Wide Web Publishing&quot; 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(&quot;oRpt&quot;).ReadRecords



If Err.Number <> 0 Then

Response.Write &quot;Error Occurred Reading Records: &quot; & Err.Description

Set Session(&quot;oRpt&quot;) = nothing

Set Session(&quot;oApp&quot;) = nothing

Response.End

Else

If IsObject(session(&quot;oPageEngine&quot;)) Then

set session(&quot;oPageEngine&quot;) = nothing

End If

set session(&quot;oPageEngine&quot;) = session(&quot;oRpt&quot;).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=&quot;SmartViewerActiveX.asp&quot; -->





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. The steps required to pass parameters or logon to secure data sources are covered in the other examples.


Encapsulating the Required Code using Include Files
You may also notice that many of the examples use two include files called AlwaysRequiredSteps.asp and MoreRequiredSteps.asp. These files encapsulate much of the essential code into files that can be easily included into an ASP page allowing for easy code reuse. A single copy of the encapsulated code can be included into any number of ASP pages.



You should be having the include file from your samples when you installed crystal reports
Also ou can connect to diff databases by using the setlogonInfo of the report object.
Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top