Detailed instructions on the use of Crystal Reports in ASP. NET

Source: Internet
Author: User

Some time ago, because reports were used for statistics in the project, I learned how to use ASP. NET Crystal Reports. The Execution Mode of the Crystal Report (that is, the Data Retrieval Method) can be divided into two types:

The first is the 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.

The other is the Push mode: At this time, the developer has to write code to connect data and assemble the 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.

There are two types of reports in the Crystal Report:

-- StrongTyped Report: when you add a report file to a project, it becomes a "stronugly-typed" report. In these cases, you will have the right to directly create report objects, which will reduce some code and provide some performance.

-- UnTyped report: the report is not directly included in the project, so it is called 'un-typed 'report. In this case, you have to use the 'reportdocumi' object of the crystal report to create an instance and use the report manually.

In the Pull mode, we will use the following steps to execute the Crystal Report in the 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.
Copy codeThe Code is as follows:
ReportDocument ReportDoc = new ReportDocument ();
ReportDoc. Load (Server. MapPath ("MyPullDemo. rpt "));
# Region solve logon errors
TableLogOnInfo logonInfo = new TableLogOnInfo ();//
Foreach (CrystalDecisions. CrystalReports. Engine. Table tb in ReportDoc. Database. Tables)
{
LogonInfo = tb. LogOnInfo;
LogonInfo. ConnectionInfo. ServerName = "(local )";
LogonInfo. ConnectionInfo. DatabaseName = "myDatabase ";//
LogonInfo. ConnectionInfo. UserID = "sa ";
LogonInfo. ConnectionInfo. Password = "sa1234 ";//
Tb. ApplyLogOnInfo (logonInfo );
}
# Endregion
CrystalReportViewer1.ReportSource = ReportDoc;

CrystalReportViewer1.DataBind ();


PUSH mode
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.
Copy codeThe Code is as follows:
ReportDocument ReportDoc = new ReportDocument ();
ReportDoc. Load (Server. MapPath ("MyPushDemo. rpt "));
String strProvider = "Server = (local); DataBase = myDatabase; UID = sa; PWD = sa1234 ";
SqlConnection MyConn = new SqlConnection (strProvider );
MyConn. Open ();
String strSel = "Select * from SaleOfCuntry ";
SqlDataAdapter MyAdapter = new SqlDataAdapter (strSel, MyConn );

-- The ds and. rpt files are connected to the database using the same DataSet structure and different names.

When a. rpt file is created, DataSet provides an architecture for the rpt file.

Enter an instance of the dataset with dataadapter as the data source of the report.
Copy codeThe Code is as follows:
DataSet ds = new DataSet ();
MyAdapter. Fill (ds, "SaleOfCuntry ");
ReportDoc. SetDataSource (ds );
Crv. ReportSource = ReportDoc;

Crv. DataBind ();

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