Basic crystal report Operation Skills

Source: Internet
Author: User
Tags microsoft website visual studio installer
Crystal Report
1. Crystal Report registration code
Register the crystal report that comes with Otherwise, you can only use it 30 times. The registration code of the crystal report is as follows:
Registration number: 6707437608
Password: AAP5GKS0000GDE100DS
2. Use Crystal Reports in Asp. Net
We use the following steps to execute the Crystal Report in Push mode:
1) Design a DataSet
Right-click solution browser, and choose add> Add> dataset ", drag and drop the "Stores" table (in the PUBS database) from "SQL Server" in "Server resource manager ). Drag the table into the dataset. The. xsd file only contains a structure chart, but there is no data in it.
2) create a. rpt file and specify it to the DataSet created in the previous step.
Use the method described above to create this file. The only difference is that you can use a dataset to directly connect to the previous data. Create. right-click the rpt file and choose "details"> "Add/delete database". In the "database experts" window, expand "project data" (instead of the previous OleDb ), expand "ADO. net dataset "--" DataSet1 ", and select the" Stores "table. Add the "Stores" table to "selected table" and click "OK ".
3) drag and drop a CrystalReportViewer control on the aspx page and associate it with the previous rpt file.
Use the method in PULL mode to create a WebForm, add a Crystal Report Viewer control, and set its attributes. This is consistent with the method in PULL mode. The Code is as follows:
Private MyReport oCR; // MyReport is the class name in MyReport. aspx. csk.
Private void Page_Load (object sender, System. EventArgs e)
DataSet1 ds = new DataSet1 ();
OCR = new MyReport ();
SqlConnection MyConn = new SqlConnection (ConfigurationSettings. etettings ["SqlConn"]);
MyConn. Open ();

// Directly access the data table
// String strSel = "select * from tbtree ";
// SqlDataAdapter MyAdapter = new SqlDataAdapter (strSel, MyConn );
// MyAdapter. Fill (ds, "tbtree ");

// Use the Stored Procedure
SqlCommand cmd = new SqlCommand ("display", MyConn );
Cmd. CommandType = CommandType. StoredProcedure;
SqlDataAdapter MyAdapter = new SqlDataAdapter (cmd );
MyAdapter. Fill (ds, "tbtree ");

OCR. SetDataSource (ds );
This. CrystalReportViewer1.ReportSource = oCR;
This. CrystalReportViewer1.DataBind ();
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.
4) access the database in the Code and store the data into DataSet
5) Export report files to other formats
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 in Pull mode. When you export a file created in Pull mode, the crystal report accurately opens the required data. First, add a <DIV> element and control to HTML.
<DIV id = "div_opt" style = "Z-INDEX: 102; LEFT: 320px; WIDTH: 360px; POSITION: absolute; TOP: 24px; HEIGHT: 32px" ms_positioning = "FlowLayout">
<Asp: linkbutton id = "lbtn_IEPrint" runat = "server"> IE print preview </asp: linkbutton> & nbsp; format conversion
<Asp: dropdownlist id = "ddl_FileFormat" runat = "server">
<Asp: ListItem Value = "pdf"> Pdf </asp: ListItem>
<Asp: ListItem Value = "doc"> Word </asp: ListItem>
</Asp: dropdownlist>
<Asp: button id = "btn_Change" runat = "server" Text = ""> </asp: button>
The following code executes the export function:
Private void lbtn_IEPrint_Click (object sender, System. EventArgs e)
If (lbtn_IEPrint.Text = "IE print preview ")
CrystalReportViewer1.SeparatePages = false;
CrystalReportViewer1.DisplayToolbar = false;
Lbtn_IEPrint.Text = "cancel IE print preview ";
CrystalReportViewer1.SeparatePages = true;
CrystalReportViewer1.DisplayToolbar = true;
Lbtn_IEPrint.Text = "IE print preview ";
Private void btn_Change_Click (object sender, System. EventArgs e)
String filetype = "";
Filetype = ddl_FileFormat.SelectedValue;
String contenttype = "";

String myfilename = Request. MapPath (". \") + Session. SessionID + "." + filetype;
CrystalDecisions. Shared. DiskFileDestinationOptions mydiskoptions = new CrystalDecisions. Shared. DiskFileDestinationOptions ();
Mydiskoptions. DiskFileName = myfilename;
CrystalDecisions. Shared. ExportOptions myExportOptions = oCR. ExportOptions;
MyExportOptions. DestinationOptions = mydiskoptions;
MyExportOptions. ExportDestinationType = CrystalDecisions. Shared. ExportDestinationType. DiskFile;

Switch (ddl_FileFormat.SelectedItem.Value)
Case "pdf ":
Contenttype = "application/pdf ";
MyExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. PortableDocFormat;
Case "doc ":
Contenttype = "application/msword ";
MyExportOptions. ExportFormatType = CrystalDecisions. Shared. ExportFormatType. WordForWindows;

OCR. Export ();

Response. ClearContent ();
Response. ClearHeaders ();
Response. ContentType = contenttype;
Response. WriteFile (myfilename );
Response. Flush ();
Response. Close ();

System. IO. File. Delete (myfilename );

2. Use Crystal Reports in C #. Net WinForm
1) register your own crystal report. Otherwise, you can only use it 30 times.
2) 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 drag-and-drop operations.
(3) When running an application, the report is displayed in the viewer.
3) create a new report
(1) point to "add" and 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:
A. Use Report experts to guide you through the Report creation process and add your selection to the Crystal Report Designer.
B. Open the Crystal Report Designer as a blank Report.
C. From the existing report-> Create a new report, which is the same as the design of another 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 "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 experts" tab, and click "finish" to access the Crystal Report Designer and your reports.
(5) 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. To provide developers with the most flexible data access methods, the Crystal Reports database driver is designed as a pull model and a push model that provides both data access.
Pull model pull: In the pull model, the driver will connect to the database and "pull" the 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 push: on the contrary, the push 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.
4) create a report from the ADO. NET Dataset
Create a DataSet object from a database
(1) create a new architecture file in the project:
In Solution Explorer, right-click the project name, point to "add", and then click "Add new project ". In the "category" Area of the "Add new project" dialog box, expand the folder and select "data ". Select "dataset" in the "template" area ". Accept the default name Dataset1.xsd. This creates a new schema file (Dataset1.xsd), which will be used to generate a strong dataset. The schema file is displayed in the ADO. NET Dataset Designer.
(2) Specify the database location:
In server resource manager, right-click "Data Connection" and select "add connection ". In the "Data Link Properties" dialog box, click the "providers" tab and select a Provider (for example, Microsoft ole db Provider for SQL Server ). Click the connection tab and specify the location of your database. Enter the server and logon information at the required location. Click OK. In this case, your database, its tables, and fields appear under the "Data Connection" node of the server resource manager.
(3) In Solution Explorer, double-click Dataset1.xsd (if it is not an active view yet ). Dataset1.xsd should now be displayed on the dataset tab.
(4) To create a schema for a dataset, drag the required table from the server resource manager to the dataset tab of Dataset1.xsd.
(5) Click "Save Dataset1.xsd" to save the "Dataset1.xsd" file.
(6) On the "generate" menu, click "generate" to generate a DataSet object for the project.
The ADO. NET DataSet object provides a description of the data from which tables can be added to the Crystal report. Use "database experts" in Crystal Report Designer to add tables from ADO. NET dataset objects. Call "Database Expert" when using "report expert" to create a new report ". Or, you must use ADO. to access "database experts" in a Report created by. NET, right-click Report Designer, point to "Database", and click "Add/delete database ". Connect a report to an ADO. NET DataSet object
(1) In "database experts", expand the "project data" folder.
(2) Expand the "ADO. NET dataset" folder.
(3) Select the expected DataSet object.
For example, if you are using a DataSet object generated from the project "Dataset1.xsd" architecture file "WindowsApplication1", you should select "WindowsApplication1.Dataset1 ". Select the table to be added to the report.

5) dynamically change the data source code
DataSet dsdataSet = new DataSet ();
RptClient () oRpt = new rptClient (); // created report rptClient
OleDbDataAdapter da = new OleDbDataAdapter ("select * from TableName", conn );
Da. Fill (dsdataSet, "TableName ");
// Use the "Report Engine" Object Model to pass the filled dataset to the report
ORpt. SetDataSource (dsdataSet. Tables (0 ));
// Bind a report object with data to Windows form viewer and rptVew (CrystalReportViewer Control)
RptVew. ReportSource = oRpt;
Note that the FillDataSet method can connect to the specified database, extract data, and then disconnect the database. If you want to add multiple tables in the database to the report, use the SQL JOIN statement to JOIN these tables. Then, specify a result table in the FillDataSet method.

6) create a master-slave report
In a report, many reports are in the master-slave table structure, such as order and order item details. Orders are one record in one table, while entries are multiple records in another table, two tables are joined by a field. This report can be implemented using its grouping function.
(1) create a project
(2) Add a CrystalReportViewer control to FORM1
(3) connect to the Northwind database on SQL SERVER 2000 in the Service Catalog Resource Manager
(4) Add a dataset Dataset1 and add Orders and Order Details from server resource manager to the dataset.
(5) Add a crystal report, use report experts, and select "ADO.. NET dataset ", insert the table Orders and Order Details. The" Link "contains links to associated fields. In the" field ", select the fields of the master table and the detail table to be displayed, the OrdersID field, total, chart, selection (filtering), and style (report title can be set) of the Orders table are selected for the group. After setting, click Finish.
(6) Adjust the position and width of the fields to be displayed in the report designer.
(7) Add code in the window.
Public PrintForm ()
InitializeComponent ();

OleDbConnection conn = new OleDbConnection ("data source = PMSERVER; initial catalog = Northwind; user id = sa; password = sa ");
Dataset1 () ds = new Dataset1 ();

OleDbDataAdapter da1 = new OleDbDataAdapter ("select * from orders", conn );
Da1.Fill (ds, "orders ");

OleDbDataAdapter da2 = new OleDbDataAdapter ("select * from [Order Details]", conn );
Da2.Fill (ds, "Order Details ");

CrystalReport1 oCR = new CrystalReport1 ();
OCR. SetDataSource (ds );
This. crystalReportViewer1.ReportSource = oCR;
(8) run the program

3. An error occurred while logging on to the Crystal Report.
Using CrystalDecisions. Shared;
// Declare the required variable.
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
// Cycle each table in the report.
For (int I = 0; I <Report. Database. Tables. Count-1; I ++)
// Set the connection information of the current table.
LogOnInfo. ConnectionInfo. ServerName = serverNameTxt. Text;
LogOnInfo. ConnectionInfo. DatabaseName = dbNameTxt. Text;
LogOnInfo. ConnectionInfo. UserID = userNameTxt. Text;
LogOnInfo. ConnectionInfo. Password = passwordTxt. Text;
Report. Database. Tables [I]. ApplyLogOnInfo (logOnInfo );
Note that this method is also used for password-protected PC databases such as Microsoft Access and Paradox, but LogOnInfo. ServerName is the path of the data file, and LogOnInfo. DatabaseName must be left blank. TableLogOnInfo is a member of the CrystalDecisions. Shared namespace. To add a reference: using CrystalDecisions. Shared ;.

4. Release of the Crystal Report Application
1) installation and deployment
During installation and deployment, enter the msm of the Crystal report and register it on the server. Deploy and use Crystal Reports for Visual Studio. NET 2003 Program, first, find the following seven Files (you can find them in The 'C: \ Program Files \ Common Files \ Merge Modules 'folder ):
VC_User_CRT71_RTL_X86 _ ---. msm
VC_User_STL71_RTL_X86 _ ---. msm
In Solution Explorer, right-click the installation project, point to "add", and then click "Merge Module ". In the "add module" dialog box, select the seven modules to be added to the installation project, and click "open ".
Also, open solution --> right-click the attribute of Crystal_regwiz2003.msm, and fill in the "License Key" in "MergeMouduleProperties: aap5gksjwgde100ds (this is the password of the registration number used to generate the Crystal Report !)

2) FAQs and solutions for deploying Crystal Reports
(1) Report loading error
The problem lies in report Publishing. A project that publishes a crystal report must contain three components: Report (*. rpt), Module (*. (*. these module Files are in the C: \ Program Files \ Common Files \ Merge Modules \ directory.
A. Report (*. rpt): the crystal report file created during programming.
B. Module (*. msm ):
Managed. this module contains CR for.. NET management module, such as CrystalDecisions and CrystalReports. engine. DLL, CrystalDecisions. web. DLL, and CrystalDecisions. windows. forms. DLL ......
Managed_chs.msm is supported by the Chinese version of this module.
Database_Access.msm this module contains the database driver, the connection with different databases, and the documents and chart components that export reports in the RTF and HTML formats.
Database_Access_chs.msm is supported in the Chinese language of this module.
Regwiz. msm registration information module is very important. You must enter the registration serial number of the crystal report in this module.
C: Runtime Library (*. msm ):
If the report file uses the dataset object of ADO. NET, the VC_CRT.msm and VC_STL.msm modules must also be included in the installation project. In addition, the \ Module Retargetable Folder \ item of the File Installation attribute of the two modules must be changed to the system directory. In the VS. NET installation project, only dotNETFXRedist_x86_chs.msm and Managed. msm modules are detected by the system. We need to manually add other modules.

(2) An error occurred while deploying Windows 98/ME: \ Load Report Failed \
This is because the query engine DLL (CRQE. dll) is not correctly registered on the customer's computer. To solve this problem, manually register CRQE. dll on the client's computer and run the following command:
Regsvr32 \ C: \ program files \ common files \ Crystal Decisions \ 1.0 \ bin \ CRQE. dll \
If CRQE. dll is not successfully registered and the problem persists, check the ATL. dll version on the customer's computer. The correct ATL. dll for Windows 98/ME version is 3.0.8449. By using any of the following three methods, you can find and register the correct version of ATL. dll.
A. Install IE6.0 on the client computer.
B. manually copy and register ATL. dll on the client computer, and then run the installation package for Crystal Reports distribution.
C. Add the ATL. msm merge module to the installation project. ATL. msm is part of Visual Studio installer 1.1. You can go to the Microsoft website

(3) run the prompt \ This field name is unknown \ On the Win98 Client \
The report works normally on the Win2000 client. when running the Report on the Win98 client, the message \ This field name is unknown \? The reason is that the field name in the SQL server database used by the report is Chinese, and the field name is changed to English field name.

(4) "query engine error" when distributed to the client"
The "installation project" should contain the following modules: VC_CRT.msm and VC_STL.msm. In addition, make sure that Msvcr70.dll and Msvcp70.dll are in the "system directory" of the customer's computer.

(5) load crpe32.dll failed
The following describes how to deploy Crystal Report 9 in cr9net_deployment.pdf:
Open the Windows application. In Solution Explorer, right-click Windows application solution, point to "add", and then click "Create Project ". In the Add new project dialog box, select "Install and deploy Project" in the "project type" pane, and select "Install Project" in the "template" pane ". Click OK.
In solution resource management, right-click the installation project, point to "add", and then click "project output ". In "add project output group", select "primary output" and click "OK ". All dependencies are automatically added, such as dotnetfxredist_x86_enu.msm and dotnetcrystalreports. msm. To exclude dotnetcrystalreports. msm from the project, right-click the module in Solution Explorer and choose exclude ". In Solution Explorer, right-click the installation project, point to "add", and then click "Merge Module ". In the "add module" dialog box, select the following modules to be added to the installation project, and click "open ":
Reportengine. msm, crnetruntime. msm, license. msm
Mapping. msm (optional. When geographic maps are used in the report)
VC_CRT.msm and VC_STL.msm (if the Windows application you deploy uses the ADO. NET dataset, the VC_CRT and VC_STL merging modules must also be included)
In Solution Explorer, right-click the license. msm file and select "properties ". In the Properties window, expand MergeModuleProperties and enter a valid license key in the license key attribute box. Note that the license key must be provided whenever the Crystal Reports application is deployed. From the "generate" menu, select "generate solution" to generate the application.

(6) An error occurred while loading the Report in ASP. NET: \ Load Report Failed \
Make sure that the local user of ASPNET has "Full Control" permission on the report folder.

(7) When the. NET 9.1 program is deployed to Windows 98, an error occurs: \ Load Report Failed \
It is because the query engine DLL (CRQE. dll) is not correctly registered on the customer's computer. To solve this problem, copy Dbghelp. dll from the development computer to the \ C: Windows \ system32 \ folder on the client computer.

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: 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.