A detailed analysis of the use of Crystal report in ASP.net C #

Source: Internet
Author: User
Tags net bug ole
1 Drag the Crystal Report Viewer control (Crystal Reports Viewer) from the WebForm toolbar to the. asp Tutorial x page.
2) pull up the Crystal Report Viewer control Properties window
3) Click [...] button to view the "data binding" property and eject the DataBinding window.
4 from the left side of the "Bindable Properties" area, select "" "
5 Select the custom binding Expression radio button and specify the file name and path for the. rpt file in the window at the bottom right, for example: "C:Program filesmicrosoft Visual Studio.netcrystal Reportssamplesreportsgeneral Businessworld Sales Report.rpt ", then" OK "
Note: The file "World Sales Report.rpt" file was created at Vs.net installation. If you specify a different directory during the installation process, you may want to confirm the correctness of the path at this point.
The steps above actually insert the following code into the ASP.net tutorial file:
<%@ register tagprefix= "CR" Namespace= "Crystaldecisions.web" assembly= "Crystaldecisions.web"%>

And:


Id= "Crystalreportviewer1"
runat= "Server" width= "350px" height= "50px"
Reportsource= ' <%# ' C:Program filesmicrosoft visual Studio.netcrystal reportssamplesreportsgeneral Sales report.rpt "%> '" >
Note: In the automatic code generated in my Vs.net official version of the Flying Knife, the style produced by ReportSource is not like this, it is:
Reportsource= "<%# c:xxxxxxxx.rpt%>"
This is wrong, there are error messages, there are two errors:
DataBind must have double quotes, so only single quotes outside
The directory separator symbol cannot be used, and you must use the
You must modify it manually according to the format described in this article, which is a vs.net bug.
6) Call the DataBind method in the Page_Load method. (Code is vb.net)
Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs)

DataBind ()

End Sub
7 Save and compile your page. Now you have a WebForm page with a crystal report embedded in it.
Note: In actual development, an error in the Inetsrv directory cannot be accessed at first, and the solution is to change the security attributes of its directory so that the user has writable permissions. Flying knife I found that the solution that the. NET system has given itself is not working, or that I am using the Windows.NET operating system for reasons.
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. Accessing the database tutorial in code and storing 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), expand Ado.net DataSet-DataSet1, and 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, 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 specify the saved file name directly

"So you have to use the Diskfiledestinationoptions object to set its Diskfilename property

"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.
Using pull Mode
We will use the following steps to execute the Crystal Report through the pull mode
1. First create the RPT file and use the Crystal Report design interface to set up some necessary data connections.
2. Drag and drop a Crystalreportviewer control to the ASPX page, set its properties to specify the. rpt file that we created in the previous step.
3. Call the DataBind method in code.
To create a. rpt file:
1 Right click on "Solution Explorer", select "Add" in the pop-up menu--"Add New Item"--> "Crystal"
2 Select the As blank Report radio button in the Crystal Reports Library, and then click OK.
3 The Crystal Report Designer will pop up here.
4) Right-click the details area in the report, select Database-> Add/Remove Database ...
5 in a pop-up database expert, expand the OLE DB (ADO) option, and an additional OLE DB (ADO) window pops 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 the name of your machine?)
User Id:sa
Password:
Database: Pubs
8 Click Next, and then click Finish.
9 then you can see the database we selected in the Database Experts window.
10 Expand the Pubs database, expand Tables, select the stores table and add it to the selected tables area, and click OK.
 
11 Now in the field resource browser, you will see the table you selected in the database fields area on the left and the fields in the table.
12 drag and drop the required fields into the details area of the report. The field name automatically appears in the header area. If you want to change the head text, you can right-click the text in the header area and select the Edit Text object option and edit it.
13 Save so we have a Crystal report file.
Creating Crystalreportviewer Controls
14) back to the front of the WebForm, drag and drop a crystal the viewer control to the page.
15 Call Crystal the Properties window of the viewer control, select "DataBindings" Area click [... ]
"Crystal" in the "List Viewer data Binding window", select "ReportSource" in bindable properties on the right, and select the. rpt file path in the lower-right corner of the custom binding expression.
17 You can now see a preview of a report file with some virtual data from the Crystal Reports Viewer control.
Note: In the above example, Crystalreportviewer can call real data directly at design time, because the data is already saved. In this case, when the data is not saved at design time, he cannot display the data. The generation of this is the display of some virtual data, only in the execution of the selection of real data.
Code behind programming
18) Call the DataBind method in the Page_Load method.
Execute your program.
19 Create and run your program!

You can now use some of the built-in features of the Crystal Report, such as page navigation, scaling, and so on, directly on the Web page.

We will use the following steps to execute the Crystal Report through the pull mode
1. First create the RPT file and use the Crystal Report design interface to set up some necessary data connections.
2. Drag and drop a Crystalreportviewer control to the ASPX page, set its properties to specify the. rpt file that we created in the previous step.
3. Call the DataBind method in code.
To create a. rpt file:
1 Right click on "Solution Explorer", select "Add" in the pop-up menu--"Add New Item"--> "Crystal"
2 Select the As blank Report radio button in the Crystal Reports Library, and then click OK.
3 The Crystal Report Designer will pop up here.
4) Right-click the details area in the report, select Database-> Add/Remove Database ...
5 in a pop-up database expert, expand the OLE DB (ADO) option, and an additional OLE DB (ADO) window pops 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 the name of your machine?)
User Id:sa
Password:
Database: Pubs
8 Click Next, and then click Finish.
9 then you can see the database we selected in the Database Experts window.
10 Expand the Pubs database, expand Tables, select the stores table and add it to the selected tables area, and click OK.
 
11 Now in the field resource browser, you will see the table you selected in the database fields area on the left and the fields in the table.
12 drag and drop the required fields into the details area of the report. The field name automatically appears in the header area. If you want to change the head text, you can right-click the text in the header area and select the Edit Text object option and edit it.
13 Save so we have a Crystal report file.
Creating Crystalreportviewer Controls
14) back to the front of the WebForm, drag and drop a crystal the viewer control to the page.
15 Call Crystal the Properties window of the viewer control, select "DataBindings" Area click [... ]
"Crystal" in the "List Viewer data Binding window", select "ReportSource" in bindable properties on the right, and select the. rpt file path in the lower-right corner of the custom binding expression.
17 You can now see a preview of a report file with some virtual data from the Crystal Reports Viewer control.
Note: In the above example, Crystalreportviewer can call real data directly at design time, because the data is already saved. In this case, when the data is not saved at design time, he cannot display the data. The generation of this is the display of some virtual data, only in the execution of the selection of real data.
Code behind programming
18) Call the DataBind method in the Page_Load method.
Execute your program.
19 Create and run your program!
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.