Crystal Report Generation

Source: Internet
Author: User
Tags ole

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

Orpt = new reportdocument ();
String rptdir = server. mappath ("myreport. rpt ");
// Response. Write (rptdir );
// String rptdir = "E: // vs2005 // aspcry.pdf // myreport. rpt"; // the absolute path of the crystalreport1.rpt File
Orpt. Load (rptdir );

Sqlconnection con = new sqlconnection ("Server =.; database = northwind; uid = sa; Pwd = ;");
Con. open ();
Sqldataadapter da = new sqldataadapter ("select * from MERs", con );
Dataset DS = new dataset ();
Da. Fill (DS, "MERs ");
Orpt. setdatasource (DS );
This. crystalreportviewer1.reportsource = orpt;

II. C #. net use Crystal Reports in winform

Using crystaldecisions. crystalreports. engine;
Using crystaldecisions. shared;
Myreport mydoc = new myreport ();
Mydoc. setdatasource (Ds. Tables ["persontb"]);
Crystalreportviewer1.reportsource = mydoc;
Textobject TB = (textobject) mydoc. reportdefinition. reportobjects ["text13"];
TB. Text = vievprint. content;

Iii. Use the PULL mode   We will use the following steps to execute the Crystal Report in PULL mode. 1. First create the 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. 1) Right-click Solution Explorer and select Add -- add new item --> Crystal Report from the pop-up menu. 2) select "blank report" from "Crystal Report library" and click "OK". 3) The Crystal Report designer is displayed. 4) Right-click "details area" in the report and choose "Database"> "Add/delete database... "5) in the pop-up" Database Expert ", expand the" ole db (ADO) "option, and an" 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 (what is your machine name?) 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 the "selected table" area, and click "OK. 11) now, in the "field resource Browser", the selected table and fields in the table are displayed in the "database Field" area on the left. 12) drag and drop the required fields to the "details" Area of the report. The field name automatically appears in the "Header" area. If you want to modify the header text, 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. Create the crystalreportviewer control 14) return to the previous webform and drag a crystal report viewer control to the page. 15) in the Properties window of the Crystal Report viewer control, select "databindings" and click [...]. 16) in the "Crystal Report viewer Data Binding window", select "reportsource" in "binable attributes" on the right, and select the custom binding expression in the lower right corner. rpt file path. In section 2005, you can directly create a new reportsourceid and select specify. Rpt. 17. In this case, you can see the preview of a report file consisting 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.

Protected void page_load (Object sender, eventargs E)
{


Tablelogoninfo logoninfo = new tablelogoninfo ();
Orpt = new reportdocument ();
String rptdir = "E: // vs2005 // aspcry.pdf // myreport. rpt"; // the absolute path of the crystalreport1.rpt File
Orpt. Load (rptdir );

Logoninfo. connectioninfo. servername = "HPC ";
Logoninfo. connectioninfo. databasename = "northwind ";
Logoninfo. connectioninfo. userid = "sa ";
Logoninfo. connectioninfo. Password = "";
Orpt. database. Tables [0]. applylogoninfo (logoninfo );
This. crystalreportviewer1.reportsource = orpt;
}

Execute your program

20. To generate a PDF file, add it to the above Code
Exportoptions crexportoptions = new exportoptions ();
Diskfiledestinationoptions crdiskfiledestinationoptions = new diskfiledestinationoptions ();
Crdiskfiledestinationoptions. diskfilename = "F: // bbs // test // crystal // crystalreport1.pdf ";
Crexportoptions = orpt. exportoptions;
Crexportoptions. destinationoptions = crdiskfiledestinationoptions;
Crexportoptions. exportdestinationtype = exportdestinationtype. diskfile;
Crexportoptions. exportformattype = exportformattype. portabledocformat;
Orpt. Export ();
Orpt. Close ();

Read the exported report content and upload it to the client? Continue to add the following code
Response. clearcontent ();
Response. clearheaders ();
Response. contenttype = "application/pdf ";
Response. writefile ("F: // bbs // test // crystal // crystalreport1.pdf ");
Response. Flush ();
Response. Close ();

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.