Summary of report Learning (1) -- simple use of ASP. NET Crystal Report (Crystal Reports), crystalreports

Source: Internet
Author: User

Summary of report Learning (1) -- simple use of ASP. NET Crystal Report (Crystal Reports), crystalreports
1. Introduction to Crystal Reports

Crystal Reports is a business intelligence (BI) software used to design and generate Reports. Crystal Report is the most professional and functional Report System in the industry. In addition to its powerful report functions. The biggest advantage is that it integrates with the vast majority of popular development tools and interfaces. Programmers who have worked in report development on VS. Net platform must have been impressed by the powerful, efficient, and integrated features of Crystal Reports. In addition to developing new programs, we often need to meet the needs of many early software system report function upgrades. If we can combine the powerful tool of Crystal Report, we can always get twice the result with half the effort.

II. Implementation Mode of Crystal Reports

Before explaining the implementation mode of the crystal report, I will first explain how to use the configuration environment of my computer:

Visual Studio 2013 and SQL Server 2012

VS2013 does not provide the function of creating a crystal report, so you need to download and install a plug-in online to use VS2013 to create a crystal report.


After the plug-in is successfully installed, open VS2013 and you will find that these items are added to the toolbox. It means that your VS2013 has installed the crystal report plug-in. Now you can create a crystal report through VS2013.

2.1. PULL (PULL) Mode

Concept Introduction: the PULL mode is directly connected to the database (source) by the crystal template (ENGINE) and pulled data from the database (source.

2.1.1. Create a crystal report PULL mode without a single piece of code.

1. Prepare the database data before creating the Crystal Report PULL mode:

use master
create database Demo
use Demo
create table Dept
   ID int primary key identity (1,1),
   DeptID nvarchar (10),
   DeptName nvarchar (10)
create table UserInfo
   ID int primary key identity (1,1),
   UserName nvarchar (10),
   Salary decimal (10,2),
   Gender bit,
   DeptID int foreign key references Dept (ID)
insert into Dept values ('HR', 'Personnel')
insert into Dept values ('DT', 'Development')
insert into UserInfo values ('Program Ape 1', 2500,1,2)
insert into UserInfo values ('Program Ape 2', 3500,0,2)
insert into UserInfo values ('Program Ape 3', 5500,0,1)
insert into UserInfo values ('Program Ape 4', 6500,1,1)
insert into UserInfo values ('Program Ape 5', 10500,1,2)

2. Open VS2013 to create an empty website project.

3. Right-click the project and add a Reports folder to manage the crystal report.

4. Right-click the folder and select Add "New Project", find "CrystalReports", and enter the report name.

5. Click the Add button. The default Crystal Report Wizard dialog box is displayed. Follow the wizard default options.

6. After confirmation, open the "standard report creation wizard" interface.

7. After the database connection is successful, your connection information will appear in the resume connection, and then select the table you want to display.

8. Select the table field you want to display.

9. You do not need to select the option that appears after you click Next. Because you do not need the option for the moment, the following page appears when you click Next to finish.

10. The above interface shows that you have created a crystal report template. Because it is directly connected to the database, we can immediately see the actual results, click Preview of the base report to display the table data of the database.

So far, we have not typed any code, and the PULL mode report has been created.

2.1.1 use the crystal report pull mode on the Web

In the previously created Project, add a Web form named CrystalReport_Pull.aspx.

Open the design page, find the report design in the toolbox, and double-click or click drag to the Web form, as shown in.

Method 1: without coding, bind the designer to the data source to display the data.

Click OK and you will see the following Web form interface.

Click source to see the following interface. The designer automatically generates the following code.

Finally, we run VS2013 to check the effect in the browser and find that nothing is displayed. Then we opened the browser debugging tool and found that the following error was reported.

To solve this problem refer to the website:

The solution is as follows:C: \ inetpub \ wwwrootThe folder under this directory, and then copy it to the root directory of your project.

Re-compile and run the browser to view the data. For example, you can see that the data is displayed normally.

Method 2: write code in the background, connect to the database to bind the data source, and display the data.

Add a new Web form. The creation steps are the same as those above. Right-click to view the source code and add the following code to implement the crystal report pull mode.

1 using CrystalDecisions.CrystalReports.Engine;
  2 using CrystalDecisions.Shared;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
10 namespace ch02
11 {
12 public partial class CrystalReport_Pull: System.Web.UI.Page
13 {
14 protected void Page_Load (object sender, EventArgs e)
15 {
16 ConfigureCrystalReports ();
19 private ReportDocument myReport;
20 private void ConfigureCrystalReports ()
twenty one         {
22 // Define the ReportDocument object and load Crystalreport1.rpt
23 myReport = new ReportDocument ();
24 string reportPath = Server.MapPath ("Reports / CrystalReport1.rpt");
25 myReport.Load (reportPath);
26 // Assign the template object to the report front-end presentation control
27 CrystalReportViewer1.ReportSource = myReport;

Same as above

2.2. PUSH mode

Concept: The application obtains data from the database (source) and then pushes the data to the Crystal Report Engine. Crystal Reports do not interact with databases.

1. To implement the Crystal Report push mode, first we need to add a dataset to the website project.


2. You can create a data table in two ways. Here we select method 2 to create a data table.

3. Drag the table to the right.

4. Design a crystal report template. Right-click the Reports folder to add a new item. The steps are the same as those in the above push mode.

5. Finally, let's write the pageCrystalReport_Push.aspxBackground code.

First, introduce the following namespace:

using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ch02
    public partial class CrystalReport_Push: System.Web.UI.Page
        protected void Page_Load (object sender, EventArgs e)
            LoadCrystalReports ();

        // Load crystal report
        private void LoadCrystalReports ()
            DataSet ds = new DataSet ();
            string connStr = @ "server = PC-201511211346 \ MSSQLSERVER2; database = Demo; uid = sa; pwd = 123456";
            using (SqlConnection conn = new SqlConnection (connStr))
                conn.Open ();
                string SQL = "select * from UserInfo";
                SqlDataAdapter sda = new SqlDataAdapter (SQL, conn);
                sda.Fill (ds, "UserInfo");
            ReportDocument rd = new ReportDocument ();
            // Get report path
            string reportPath = Server.MapPath ("Reports / CrystalReport2.rpt");
            rd.Load (reportPath);
            // Binding data set, pay attention to a table with a data set.
            rd.SetDataSource (ds);
            CrystalReportViewer1.ReportSource = rd;

Finally, run the command to view the effect:

Summary:This article is my reference to ASP. NET Crystal Reports provides a learning summary to help beginners who want to learn the Crystal report, so that they can take less detours.

Related Article

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.