Use Crystal Report in asp.net (next)

Source: Internet
Author: User
Asp.net| Crystal Report using push mode
We used the following steps to execute the Crystal report using push mode:

1. Design a DataSet

2. Create a. rpt file and assign it to the dataset that was established in the previous step.

3. Drag and drop a Crystalreportviewer control in the ASPX page and connect it to the previous RPT file.

4. Access the database in code and save the data in a dataset

5. Call the DataBind method.

Design a DataSet

1) Right-click Solution Explorer and select Add--Add New Item--> DataSet


2 drag and drop the Stores table from SQL Server in Server Explorer (located in the pubs database).





3 There will be a chart of the stores table in the dataset at this time.



-The. xsd file contains only one structure diagram, but no data is in it.

To create a. rpt file:

4 Use the method described above to create this file, the only difference is to use the dataset to replace the previous direct connection data.

5 after creating the. rpt file, right-click the "Details"--> "Add/Remove Database"

6 in the Database Experts window, expand project data (instead of previous OLE DB), and expand ADO. NET DataSet "--" DataSet1 ", select the" Stores "table.

7 Add the "Stores" table to the selected table and click OK



8 using the method of Pull mode to establish a WebForm

Create a Crystal the Viewer control

9 Create a crystal and set its properties, which is consistent with the pull mode.

Code behind codes:

10 Use the following child function 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 design mode.

Myda.fill (myDS, "Stores")

' You have to use the same name as the dataset in front of you.

Dim orpt as New CrystalReport1 ()

' Crystal Report binding

Orpt.setdatasource (myDS)

' Set the Crystal Report ReportSource

Crystalreportviewer1.reportsource = Orpt

End Sub



C # code:

private void Bindreport ()

{

String strprovider = "server= (local);D atabase=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 should pay attention to the ORPT is the "strongly Typed" report file. If you need to use the "untyped" report, you have to use the Reportdocument object and then call the report file.

Run your program.

11. Run your Program

Export a report file to another format

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 using pull mode

When exporting a file created using the pull mode, the Crystal Report accurately opens the required data, and the following is the code that performs the export function:

C # code:

Vb. NET code:

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

Dim Myreport as CrystalReport1 = New CrystalReport1 ()

' Note: Here we build an example of a strong-typed Crystal report.

Dim diskopts as CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions ()

MyReport.ExportOptions.ExportDestinationType = CrystalDecisions. [Shared]. Exportdestinationtype.diskfile

' This option is also required when exporting to other files

' such as Microsoft Exchange, MAPI, and so on.

MyReport.ExportOptions.ExportFormatType = CrystalDecisions. [Shared]. Exportformattype.portabledocformat

' Here we export it as a. pdf file, you can also select other types of files above

Diskopts.diskfilename = "C:\Output.pdf"

' If you don't specify the exact directory, then the file will be saved to the [Windows]\system32 directory].

MyReport.ExportOptions.DestinationOptions = diskopts

' The Crystal Report file does not contain a direct filename attribute, so you cannot specify the saved file name directly

' So you have to use the Diskfiledestinationoptions object to set its diskfilename properties

' For the path you want, the final destinationsoptions attribute of the Crystal Report is specified as above diskfiledestinationoption

Myreport.export ()

' The code above will complete the export work.

End Sub



Export Crystal Reports using push mode

When the exported report is created by the push mode, the first step is to programmatically build the connection and assemble the dataset to set the Setdatasource property of the report. The following steps will have the same pull mode.




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.