Crystal Report (1)

Source: Internet
Author: User
Before we studied Crystal Reports in vs. net, my friends and I were very curious about how to add this complicated stuff to our web application. A week later, after reading a large number of howto documents, we successfully added some simple reports to our Asp.net Program And get some tips.

This articleArticleIt teaches you how to use Crystal Reports in. NET web applications, and also helps you avoid detours during your learning process. To get the best results, you 'd better have some basic ASP. NET database access knowledge and development experience using vs. net.

Introduction

Crystal Reports can be obtained in many ways, one of which is created using vs. net. It provides a rich set of models so that we can operate on attributes and methods at runtime. If you are using vs. Net to develop a. Net program, you do not need to install other software because it is already embedded in vs. net.
Advantages:
Vs. NET Crystal Reports have the following main advantages:
Quick report development
Ability to export complex interactive charts
It can be used with other controls in webform.
Reports can be imported into multiple formats, such as. txt, xls, HTML, and RTF.
Structure:
Some components form a layer-2 structure of the crystal report. The following web applications are required:
Client:

The client only needs a browser that can access the reports embedded in the ASPX page.
Server:

-Crystal Report Engine (crengine. dll ))

It can be used to complete some tasks, such as merging data in the report file and converting the report to another format. It is precisely because of the role of the Report Engine that the crystal report in ASP. NET can be converted into a common HTML format.

-Crystal Report designer (crdesigner. dll ))

The crystal report is created in the designer. In the designer, you can design titles, insert data, formulas, charts, subreports, and so on.

-. Rpt report file

The first step in executing the report is to create the Report on the interface of the Crystal Report designer. By default, Microsoft has provided some ready-made. rpt examples.

-Data Source

The method for getting the. rpt file to the database depends on your method selection. You can choose to allow the Crystal Report to select data by yourself without using anyCodeAlternatively, you can manually assemble the dataset and then transmit it to the report file.

-Crystal Report viewer web form control (crwebformviewer. dll ))

The crystal report display control is a webform control that can be viewed as a container for storing reports on the. ASPX page. Note: In some complex operations, the Report Server and the Web server may not be on the same physical host, and the Web server sends HTTP requests to the Report Server. Crystal Reports can also be executed as WebServices.
Execution Mode
The following method can be used to retrieve data from a crystal report:
-Pull Mode:
When a request is sent, the crystal report directly connects to the database based on the specified driver and then assembles the data.
-Push mode:
At this time, the development table has to write code to connect data and assemble dataset, and send it to the report. In this case, you can use the report to maximize performance by sharing connections and limiting the size of record sets.

report type:
the crystal report designer can directly include reports to projects or use independent report objects.
-stronugly-typed report:
when you add a report file to a project, the report file is changed to a report file, stronugly-typed, and report file. In these cases, you will have the right to directly create report objects, which will reduce some code and provide some performance.
-un-typed report:
the report is called 'un-typed 'report because it is not included in the project. In this case, you have to use the "reportdocuemts; object of the crystal report to create an instance and" manually "report.

Other considerations
-Although the Crystal Report viewer has some cool functions, such as scaling and page navigation. However, it does not provide the printing function. You have to call the printing function of the browser.
-If the Crystal Report in vs. NET is not registered, it can only be used for 30 times. After 30 times, the "Save;" function can no longer be used. To avoid this, you are not registering this product at http://www.crystaldecisions.com. (It seems that this is not the case. If you do not register, it may take a long time, but you cannot provide support)
-The default crystal report can only support five users. To support more users, you have to purchase a license at http://www.crystaldecisions.com.

Let's take a look ---- use a ready-made crystal report file in Asp.net
1) drag the Crystal Report viewer control from the webform toolbar to the. ASPX page.
2) display the Properties window of the Crystal Report viewer Control
3) Click [...] to view the "Data Binding" attribute. The databinding window is displayed.
4) select from the "Bindable attributes" area on the left; report source"
5) Select the "custom binding expression" radio button and specify it in the window at the bottom of the right. rpt file name and path, 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;" is created when vs. NET is installed. If you specify other directories during installation, you 'd better confirm the correctness of the path.
In the above steps, the following code is inserted into the ASP. NET file:

And:

Id = "crystalreportviewer1"

Runat = "server" width = "350px" Height = "50px"

Reportsource = ''> ''〉


Note: In the code automatically generated in the official vs. Net version of Apsara stack, the style generated by reportsource is not like this. It is:

Reportsource = ""
The error message is displayed. There are two errors:
It must be manually modified according to the format described in this article. This is also a bug in vs. net.
6) Call the databind method in the page_load method. (The code is 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 built-in Crystal Reports.
Note: In actual development, the inetsrv directory cannot be accessed at the beginning. The solution is to change the Security Attribute of the directory so that the user has writable permissions. Flying knife I found that the solution provided by the. NET system is useless, or it may be because I am using the Windows. Net operating system.

Use Crystal Reports (medium) in ASP. NET)
Www.dotnet8.com 2002-9-6 DOTNET

The following is the program code:
Use the PULL mode
We will use the following steps to execute the Crystal Report in PULL mode.
1. Create an rpt file and use the crystal report design interface to set some required data connections.
2. Drag and Drop a crystalreportviewer control to the ASPX page and set its properties to specify the. rpt file we created in the previous step.
3. Call the databind method in the code.
Create a. rpt file:
1) Right-click Solution Explorer, and select Add; -- add new item in the pop-up menu. --> "Crystal Report"

2) Select "as blank report in" Crystal Report library ". Select the single button and click; OK ;.

3) The Crystal Report designer is displayed.

4) Right-click Details area in the report and select database> Add/delete database ..."
5) in the pop-up "Database Expert;, expand" ole db (ADO); Option, another "ole db (ADO)" window will pop 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 (name of your machine)
User ID: SA
Password:
Database: pubs
8) Click "next; and then click" finish.
9) Then you can see the selected database in the Database Expert window.
10) Expand "pubs; database, expand" table;, select "stores; table and add it to" selected table; "area, and click" OK.

11) now, in the "field resource Browser", the "database Field" on the left is displayed. The selected table and fields in the table are displayed in the "database Field" area.
12) drag and drop the required fields to the "details" section of the report. The field name will automatically appear in the "header;" area. If you want to modify the header text, right-click "Header", select "edit text object", and select "edit text object.

13) Save, so we have a crystal report file.
Create the crystalreportviewer Control
14) return to the front webform and drag a crystal report viewer control to the page.
15) in the Properties window of the Crystal Report viewer control, select; In the databindings section, click [...]
16) in the "Crystal Report viewer Data Binding window", select "reportsource;" in the "" on the right, and select the lower-right corner. The custom binding expression is specified. rpt file path.
17) in this case, you can view the preview of a report file composed of some virtual data in the Crystal Report viewer control.
Note: In the preceding example, crystalreportviewer can directly call real data during design because the data has been saved. In this case, data cannot be displayed when no data is saved during design. In this case, some virtual data is displayed, and only real data is selected during execution.
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 directly use some built-in functions of the Crystal Report on the web page, such as page navigation and scaling.

Use Crystal Reports (below) in ASP. NET)
Www.dotnet8.com 2002-9-6 DOTNET

[HTML] we use the following steps to execute the Crystal Report in Push mode:
1. Design a dataset
2. Create a. rpt file and specify it to the dataset created in the previous step.
3. Drag and Drop a crystalreportviewer control on the ASPX page and associate it with the previous rpt file.
4. Access the database in the Code and store the data into Dataset
5. Call the databind method.
Design a dataset
1) Right-click solution browser, and select Add -- add new item --> dataset"

2) drag and drop from; server resource manager; SQL Server; Stores Table (in the pubs database ).

3) A structure chart of the stores table is displayed in the dataset.

-The. XSD file only contains a structure chart, but no data is contained in it.
Create a. rpt file:
4) use the method described above to create this file. The only difference is that you can use a dataset instead of directly connecting to the previous data.
5) after the. rpt file is created, right-click the file and choose "details"> "Add/delete database;
6) in the Database Expert window, expand project data (instead of the previous oledb), expand; ADO. Net dataset "--" dataset1;, and select "stores; table.
7) add the "stores" table to the selected table, and click "OK"
8) use the method in PULL mode to create a webform
Create a crystal report viewer Control
9) create a crystal report viewer control and set its properties. The control is consistent with that in PULL mode.
Code behind code:
10) use the following sub-functions 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 use in the design mode.

Myda. Fill (myds, "stores ")

'You have to use the same name as dataset.

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); database = 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 must note that orpt is a report file of "strongly typed. To use a "untyped" report, you must use the reportdocument object and then call the report file.
Run your program.
11) run your program


Export report files to other formats

You can export a report file to the following format:

1. pdf (Portable Document Format)
1. 2. DOC (MS Word Document)
2. xls (MS Excel spreadsheet)
3.2. html (Hyper Text Markup Language-4.0 or compliant)
4. 5. rtf (Rich Text Format)

Export a report in PULL mode
When you export a file created in PULL mode, the crystal report accurately opens the required data. The following code executes the export function:
C # code:
VB. NET code:
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click

Dim myreport as crystalreport1 = new crystalreport1 ()

'Note: here we create a strong-typed crystal report instance.

Dim diskopts as crystaldecisions. Shared. diskfiledestinationoptions = new crystaldecisions. Shared. diskfiledestinationoptions ()

Myreport. exportoptions. exportdestinationtype = crystaldecisions. [Shared]. exportdestinationtype. diskfile

'This option is also required when exporting to other files.

'Such as Microsoft Exchange and mapi.

Myreport. exportoptions. exportformattype = crystaldecisions. [Shared]. exportformattype. portabledocformat

'Here, we export the file to the ultimate format. You can also select other types of files above.

Diskopts. diskfilename = "C: \ outputfolder"

'If you do not specify the exact directory, the file will be saved to the [windows] \ system32 directory.

Myreport. exportoptions. destinationoptions = diskopts

'The crystal report file does not contain the direct filename attribute, so you cannot directly specify the saved file name.

'So you have to use the diskfiledestinationoptions object to set its diskfilename attribute

'The path you want, and finally specify the destinationsoptions attribute of the crystal report as diskfiledestinationoption

Myreport. Export ()

'The above code will complete the export.
End sub

Export a crystal report in Push mode
When the exported report is created in Push mode, the first step is to establish a connection through programming and assemble dataset to set the setdatasource attribute of the report. In the following steps, the PULL mode is the same.
[/Html]
Summary of use of Crystal Reports in. Net Environment

Crystal report is an excellent report development tool. When I develop a general management system, all reports use Crystal Reports. Its simplicity, ease of use, and powerful functions have become a favorite of the author, now we will present the crystal report to you by hand.

1. register your own crystal report. Otherwise, you can only use it 30 times.

Crystal Report registration code
Registration number: 6707437608
Password: aap5gks0000gde100ds

Ii. Use crystalreportviewer for preview

The crystalreportviewer control allows you to view Crystal Reports in applications. The reportsource attribute is used to set the report to be viewed. After this attribute is set, the report is displayed in the viewer. The report source can be a reportdocument or report file path, or a strongly typed report.

1. Open the toolbox and drag a crystalreportviewer to the form. we name it rptvew.

2. Adjust the Windows form viewer to the desired size and move it to the desired location through the drag-and-drop operation.

3. When an application is running, the report is displayed in the viewer.

3. Create a new report

1. Point to; add ", click; Add new item ".

2. In the; Add new project dialog box, select crystal report from the; Template area, name the report rptclient, and click open ".

3. In the Crystal Report library, select one of the following options:

· Use report experts to guide you through the report creation process and add your selection to the Crystal Report designer.

· As a blank report-open crystal report designer.

· From an existing report-create a report, which is designed to be the same as another specified report.

Note that the crystal report library contains many experts who can guide you through the creation of several specific types of reports. You may want to use experts to create the initial report to determine which report construction method is suitable for your needs.

4. Click OK.

If you select to use report expert, the report expert dialog box appears with the data resource manager. Select the required data for each folder, complete the operations on the report expert tab, and click Finish to access the Crystal Report designer and your reports.

4. Do I need to dynamically set the data source?

Crystal Reports connects to the database through the database driver. Each driver is programmed to process a specific database type or database access technology.

Pull and push models
To provide developers with the most flexible data access methods, the Crystal Reports database driver is designed to provide both a pull model and a push model for data access at the same time.

Pull Model

In the PULL model, the driver connects to the database and pulls data as needed. When using this model, the connection to the database and the SQL commands executed to obtain data are both processed by Crystal Reports, and developers do not need to write code. If you do not need to write any special code at runtime, use the PULL model.

Push Model

On the contrary, the pushing model requires developers to write code to connect to the database, execute SQL commands to create a record set or dataset that matches the fields in the report, and pass the object to the report. This method allows you to place the connection share in the application and filter the data before Crystal Reports receives the data.

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.