Use Crystal Reports (below) in ASP. NET)

Source: Internet
Author: User
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. InCodeAccess the database and store the data in Dataset

5. Call the databind method.

Design a dataset

1) Right-click solution browser and choose "add"> "Add new item"> "dataset"

2) drag and drop the "stores" table (in the pubs database) from "SQL Server" in "server resource manager ).

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 creating the. rpt file, right-click "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)

''Sets 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 yourProgram.

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)

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

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.