Using Crystal Reports in asp.

Source: Internet
Author: User
Tags ole

Using Crystal Reports in ASP (top)

Before we studied Crystal Reports in vs.net, my friends and I were very curious about how to add this complex to our web Application. A week later, after reading a large number of "HOWTO" documents, we succeeded in adding some simple reports to our asp. net program, and got a few tips.

This article teaches you how to use Crystal Reports in. Net Web applications, and also allows you to take fewer detours in your learning Process. To get the best results, it is best for readers to have some basic knowledge of asp. net access to the database and the development experience of using Vs.net.

Brief introduction

Crystal Reports can be obtained in a number of ways, one of which is created using vs.net, which provides a very rich model to enable us to manipulate properties and methods at run Time. If you're using vs.net to develop A. Net program, then you don't need to install any more software, because He's already embedded in the Vs.net.

Advantages:

Vs. NET Crystal Reports have some of the following key benefits:

Rapid Report Development

Ability to export as complex interactive diagrams

Can be used with other controls in WebForm

Ability to dynamically export reports to multiple formats such as. PDF,.DOC,XLS,HTML,RTF

Structure:

Some components make up the two-tier structure of the crystal report, and the Web applications you need are:

Client:

The client only needs a browser that can access the embedded ASPX page report.

Server:

Crystal Report Engine (CREngine.dll)

It allows you to accomplish tasks such as merging data in a report file, converting reports to other formats, and so On. It is also because of the role of the reporting engine that the Crystal Reports in asp. NET can be converted into normal HTML format

Crystal Report Designer (CRDesigner.dll)

Crystal Reports are created in the designer, and in the designer you can design headings, insert data, formulas, charts, subreports, and so On.

. RPT Report File

The first step in the execution of the report is to create this report on the Crystal Report Designer interface, which Microsoft has provided in the default installation for some of the Ready-made. rpt Examples.

Data Source

The method by which The. rpt file gets the database depends on the choice of your method, and you can choose to have the Crystal report choose your data without using any code, or you can manually assemble the dataset and then transfer it to the report File.

Crystal Report Viewer Controls (crystal Reports Viewer Web Form control (CRWebFormViewer.dll))

Crystal Report The View control is a WebForm control that can be thought of as a container that holds reports in. aspx pages. Note: in some complex operations, the report server and the Web server may not be on the same physical host, and the Web server will route HTTP requests to the report Server. Crystal Reports can also be executed as Webservice.

Execution mode

The Crystal report fetch data can be implemented using the following methods:

Pull Mode:

When requested, the Crystal Report connects the database directly to the specified drive and assembles the Data.

Push mode:

At this point, you have to write your own code to connect the data and assemble the dataset, and transfer it to the Report. In these cases, you can use report performance maximization by using Connection Sharing and limiting the size of the collection of RECORDS.

Report Type:

The Crystal Report Designer is able to include reports directly to the project and can use separate report Objects.

strongly-typed report:

When you add a report file to your project, it becomes a "strongly-typed" report. In these cases, you will have the power to directly create the object of the report, which will reduce some code and provide some performance.

un-typed report:

The reports here are not directly included in the project, so they are called ' un-typed ' Reports. In this case, you have to use the "reportdocuemt" object of the Crystal report to create an instance and "manually" wither the Report.

Other precautions

Although the Crystal Report Viewer has some cool features, such as zooming, page navigation, and so On. But he doesn't provide the print function, you have to call the Browser's print Function.

Vs. NET Crystal Report if not registered, then it can only use 30 times, 30 times, "save" function can no longer be used. To avoid this, you are not registering this product here in http://www.crystaldecisions.com/. (it seems that it is not like this, do not register also seems to be able to use a long time, but can not provide support)

The Crystal Reports installed by default can only support 5 users, and in order to support more users, you will have to purchase licenses in http://www.crystaldecisions.com/.


Let's feel it---using a ready-made Crystal report file in asp.

Let's first feel the use of Crystal Reports in Webform.

1) from the WebForm toolbar, drag the Crystal Report Viewer control (from The Crystal Reports Viewer) to The. aspx page.

 

2) bring up the Properties window of the Crystal Report Viewer control

3. Tap [...] ] button to view the "Data Binding" property and pop up the databinding window.

4) Select "report Source" from the "bindable properties" area on the left

5) Select the custom binding Expression radio button and specify the file name and path for The. rpt file in the window at the bottom right, for example: "c:\\program Files\\Microsoft Visual Studio.net\\crystal reports\\ Samples\\reports\\general business\\world Sales report.rpt ", then" OK "

Note: the file "world Sales report.rpt" file was created at Vs.net Installation. If you specify a different directory during the installation process, you may want to check the correctness of the path at this Point.

The above steps actually insert the following code into the asp. net File:

<%@ Register tagprefix= "cr" namespace= "crystaldecisions.web" assembly= "crystaldecisions.web"%>

And:


Id= "CrystalReportViewer1"
runat= "server" width= "350px" height= "50px"
Reportsource= ' <%# ' c:\\program Files\\Microsoft Visual Studio.net\\crystal Reports\\samples\\reports\\general Business\\world Sales report.rpt "%>"

Note: in the auto-generated code in my vs.net version of the throwing knife, the ReportSource produces a style that is not like this, it is:

Reportsource= "<%# C:\xxxxx\xxx.rpt%>"

This is wrong, there is an error message, there are two errors:

There are double quotes in the databind, so only single quotes can be used externally
The directory separator symbol cannot use "\" and must use "\ \"
Must be manually modified in accordance with the format described in this article, which is a bug of Vs.net.

6) call the DataBind method in the Page_Load Method. (code Vb.net)

Private Sub Page_Load (ByVal sender as system.object, ByVal e as System.eventargs)

DataBind ()

End Sub


7) save and compile your Page. Now you have a WebForm page with a crystal report embedded in it.

Note: in Real-world development, There is an error that cannot access the Inetsrv directory at first, and the workaround is to change the security attributes of its directory so that user users have writable Permissions. Throwing Knives I found. NET System Self-given solution is useless, it may be that I am using the Windows.NET operating system Reasons.

Using Crystal Reports (medium) in asp.


Using pull Mode

We will use the following steps to execute the Crystal Report via pull mode

1. First create the RPT file and use the Crystal Report design interface to set up some necessary data connections.

2. Drag and drop a Crystalreportviewer control to the ASPX page, set its properties to specify The. rpt file that we created in the previous Step.

3. Call the DataBind method in your Code.

Create An. Rpt File:

1) Right-click on "solution explorer" and select "add"-"add new Item"-"Crystal report" in the popup menu

2) Select the As blank Report radio button in the Crystal report library, and then click Ok.

3) The Crystal Report Designer will pop up Here.

4) Right-click on "details area" in the report, select "database", "add/remove database ..."

5) in the pop-up database expert, expand the OLE DB (ado) option, and another OLE DB (ado) window pops up.

6) in the OLE db (ADO) pop-up window, select "Microsoft OLE DB Provider for SQL Server" and then "Next"

7) Specify the connection information

Server: ASPCN (what is your machine name?)
User Id:sa
Password:
Database: Pubs

8) Click "Next" and finally click "Finish" Button.

9) you will then be able to see the database of our choice in the database experts window.

10) Expand the "Pubs" database, expand "tables", Select the "Stores" table and add it to the "selected tables" area, click the "ok" button.

 

11) in the field explorer, you will now see the table you selected in the database fields area on the left and the fields in the Table.

12) drag and drop the required fields into the "details" area of the Report. The field name will automatically appear in the header Area. If you want to modify the head text, you can right-click the text in the header area, Select the Edit Text object option, and edit it.

13) save, So we have a crystal report File.

Creating Crystalreportviewer Controls

14) back to the previous webform, drag and drop a Crystal Report viewer control to the Page.

15) bring up the Properties window of the Crystal Report Viewer control and select the "databindings" area to click [... ]

In the Crystal report Viewer data binding window, Select ReportSource in the bindable properties on the right and select the path to The. rpt file in the lower-right corner of the custom binding Expression.

17) you can see a preview of a report file that consists of some virtual data from the Crystal Reports Viewer control at this Point.

Note: in the example above, Crystalreportviewer can invoke the real data directly at design time, because the data is saved at this Time. In this case, when the data is not saved at design time, He cannot display the Data. This is the display of some virtual data, and only the actual data is selected at execution Time.

Code Behind Programming

18) call the DataBind method in the Page_Load Method.

Execute your Program

19) Create and run your program!


You can now use some of the features built into the Crystal Report directly in the Web page, such as page navigation, zooming, and so On.


We will use the following steps to execute the Crystal Report via pull mode

1. First create the RPT file and use the Crystal Report design interface to set up some necessary data connections.

2. Drag and drop a Crystalreportviewer control to the ASPX page, set its properties to specify The. rpt file that we created in the previous Step.

3. Call the DataBind method in your Code.

Create An. Rpt File:

1) Right-click on "solution explorer" and select "add"-"add new Item"-"Crystal report" in the popup menu

2) Select the As blank Report radio button in the Crystal report library, and then click Ok.

3) The Crystal Report Designer will pop up Here.

4) Right-click on "details area" in the report, select "database", "add/remove database ..."

5) in the pop-up database expert, expand the OLE DB (ado) option, and another OLE DB (ado) window pops up.

6) in the OLE db (ADO) pop-up window, select "Microsoft OLE DB Provider for SQL Server" and then "Next"

7) Specify the connection information

Server: ASPCN (what is your machine name?)
User Id:sa
Password:
Database: Pubs

8) Click "Next" and finally click "Finish" Button.

9) you will then be able to see the database of our choice in the database experts window.

10) Expand the "Pubs" database, expand "tables", Select the "Stores" table and add it to the "selected tables" area, click the "ok" button.

 

11) in the field explorer, you will now see the table you selected in the database fields area on the left and the fields in the Table.

12) drag and drop the required fields into the "details" area of the Report. The field name will automatically appear in the header Area. If you want to modify the head text, you can right-click the text in the header area, Select the Edit Text object option, and edit it.

13) save, So we have a crystal report File.

Creating Crystalreportviewer Controls

14) back to the previous webform, drag and drop a Crystal Report viewer control to the Page.

15) bring up the Properties window of the Crystal Report Viewer control and select the "databindings" area to click [... ]

In the Crystal report Viewer data binding window, Select ReportSource in the bindable properties on the right and select the path to The. rpt file in the lower-right corner of the custom binding Expression.

17) you can see a preview of a report file that consists of some virtual data from the Crystal Reports Viewer control at this Point.

Note: in the example above, Crystalreportviewer can invoke the real data directly at design time, because the data is saved at this Time. In this case, when the data is not saved at design time, He cannot display the Data. This is the display of some virtual data, and only the actual data is selected at execution Time.

Code Behind Programming

18) call the DataBind method in the Page_Load Method.

Execute your Program

19) Create and run your program!


You can now use some of the features built into the Crystal Report directly in the Web page, such as page navigation, zooming, and so On.


  

Using Crystal Reports in ASP (bottom)


Using push mode
We use the following steps to perform a crystal report using push mode:

1. Designing a DataSet

2. Create A. Rpt file and assign it to the dataset established in the previous Step.

3. Drag and drop a Crystalreportviewer control in the ASPX page and connect it to the previous RPT File.

4. Accessing the database in code and storing the data in a dataset

5. Call the DataBind Method.

Design a DataSet

1) Right click on "solution browser", Select "add"--"add new Item"--"data set"


2) drag and drop the Stores table (located in the pubs database) from SQL Server in Server Explorer.

3) in the data set there will be a structure diagram of the stores Table.

-the. xsd file contains only one structure diagram, but there is no data inside.

Create An. Rpt File:

4) using the method described above to create this file, the only difference is to use the data set instead of the previous direct connection Data.

5) after creating The. rpt file, right-click on "details"-"add/remove database"

6) in the database experts window, expand Project Data (in place of the previous OLE db), and expand ADO. NET DataSet "--" DataSet1 ", Select the" Stores "table.

7) Add the "Stores" table to "selected tables" and click "ok"

8) Use the Pull mode method to create a WebForm

Create a Crystal Report Viewer control

9) Create a Crystal Report Viewer control and set its properties, which are consistent with pull Mode.

Code Behind:

10) use the following sub-function in the Page_Load method:

Vb. NET Code:

Sub Bindreport ()

Dim myconnection as New sqlclient.sqlconnection ()

myconnection.connectionstring= "server= (local) \netsdk;database=pubs; Trusted_connection=yes "

Dim mycommand as New sqlclient.sqlcommand ()

Mycommand.connection = myconnection

myCommand.CommandText = "select * from Stores"

myCommand.CommandType = CommandType.Text

Dim Myda as New sqlclient.sqldataadapter ()

Myda.selectcommand = mycommand

Dim MyDS as New Dataset1 ()

"this is the dataset we used in design mode

Myda.fill (myDS, "Stores")

"you have to use the same name as the dataset in front of You.

Dim orpt as New CrystalReport1 ()

"crystal Report Binding

Orpt.setdatasource (myDS)

"set the ReportSource of the Crystal report."

Crystalreportviewer1.reportsource = Orpt

End Sub

C # code:

private void Bindreport ()

{

String strprovider = "server= (local);D atabase=pubs; uid=sa; Pwd= ";

CrystalReport1 OCR = new CrystalReport1 ();

Dataset1 ds = new Dataset1 ();

SqlConnection myconn = new SqlConnection (strprovider);

MyConn.Open ();

String Strsel = "select * from Stores";

SqlDataAdapter myadapter = new SqlDataAdapter (strsel,myconn);

Myadapter.fill (ds, "stores");

Ocr.setdatasource (ds);

This. Crystalreportviewer1.reportsource = oCR;

}

Note: in the above code, you have to note that Orpt is the "strongly Typed" report File. If you need to use the "untyped" report, you have to use the Reportdocument object before you invoke the report File.

Run your Program.

11) Run your Program

Export a report file to another format

You can export a report file to the following format:

1. PDF (portable Document Format)

2. DOC (MS Word Document)

3. XLS (MS Excel Spreadsheet)

4. HTML (Hyper Text Markup language–3.2 or 4.0 Compliant)

5. RTF (rich Text Format)

Export a report using pull mode

When exporting a file created using pull mode, The Crystal report opens exactly the data you need, following the code that performs the export function:

C # code:

Vb. NET Code:

Private Sub button1_click (ByVal sender as system.object, ByVal e as System.eventargs) Handles Button1.Click

Dim Myreport as CrystalReport1 = New CrystalReport1 ()

"note: Here we create an example of a strong-typed crystal Report.

Dim diskopts as CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions ()

MyReport.ExportOptions.ExportDestinationType = Crystaldecisions. [Shared]. Exportdestinationtype.diskfile

"this option is also required when exporting as a file

"such as Microsoft Exchange, mapi, and so On.

MyReport.ExportOptions.ExportFormatType = Crystaldecisions. [Shared]. Exportformattype.portabledocformat

"here we export to Be. PDF file, You can also select other types of files above

Diskopts.diskfilename = "c:\Output.pdf"

"if you don't specify the exact directory, then the file will be saved to the [windows]\system32 directory].

MyReport.ExportOptions.DestinationOptions = diskopts

The Crystal Report file does not contain the direct filename property, so you cannot specify the saved file name directly

"so you have to use the Diskfiledestinationoptions object to set its Diskfilename property

"for the path you want, and finally specify the Destinationsoptions property of the Crystal Report as the above diskfiledestinationoption

Myreport.export ()

The above code will complete the export Work.

End Sub

Exporting Crystal Reports using push mode

When the exported report is established by the push mode, the first step is to programmatically establish the connection and assemble the dataset, setting the Setdatasource property of the Report. The next step is the same as pull Mode.

Using Crystal Reports in asp.

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.