Crystal Report demonstration-use PUSH model
The following describes how to use the PUSH model to implement Crystal Reports.
1. Create a design-time Dataset
2. Create a. rpt file and point it to the dataset we created earlier.
3. Place the Crystal Report viewer control on the. ASPX page and set its properties to point to the. rpt file created in the previous step.
4. Write the database connection function in code behind page.
5. Add the databind method.
Create a design-time dataset to define the fielsds of reports.
1) Right-click "Solution Explorer" and choose "add" --> select "Add new item --> select" dataset"
2) drag the "stores" table from "SQL Server" on the "Server Explorer" panel.
3) This will create a "stores" table in dataset.
The. XSD file created in this way only contains the definition of field, and there is no data in it. You need to create a connection to the database and fill in the data.
Create a. rpt file
4) create a. rpt file. The only difference from the previous one is that we will use dataset to create a table instead of using crystal report.
5) after creating the. rpt file, right-click "details" section and select "Add/Remove Database"
6) in the "Database Expert" window, expand "project data", expand "ADO. Net dataset", "dataset1", and select "stores" table.
7) Click ">" include "stores" table into "Selected tables"
8) Next, set the report layout.
Create a crystal report viewer Control
9) The next step is to use the PULL model to create a crystal report viewer control and set its attributes.
Change code behind pageCode:
10) design the following for your page load:Program:
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 our dataset created at design time
Myda. Fill (myds, "stores ")
'You have to use the same name as that of your dataset that you created during design time
Dim orpt as new crystalreport1 ()
'This is the crystal report file created at design time
Orpt. setdatasource (myds)
'Set the setdatasource property of the report to the dataset
Crystalreportviewer1.reportsource = orpt
'Set the crystal report viewer's property to the orpt report object that we created
End sub
Note: In the above Code, you may notice that the orpt object is an instance of "strongly typed" report file. If we use "untyped" report, we will have to use the reportdocument object and manually load the report file.
Run your program
11) Run F5.
Output report file to another format
You can select the following format to output your report file:
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)
In fact, you can place a button to trigger an output function.
Output A report file created with the PULL model
When a report file created using the PULL model is output, crystal report is sensitive to database connection and required records. Therefore, you can only use the following 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: we are creating an instance of the stronugly-typed crystal report file here.
Dim diskopts as crystaldecisions. Shared. diskfiledestinationoptions = new crystaldecisions. Shared. diskfiledestinationoptions ()
Myreport. exportoptions. exportdestinationtype = crystaldecisions. [Shared]. exportdestinationtype. diskfile
'You also have the option to export the report to other sources
Like Microsoft Exchange, mapi, etc.
Myreport. exportoptions. exportformattype = crystaldecisions. [Shared]. exportformattype. portabledocformat
'Here we are exporting the report to A. pdf format. You can
'Also choose any of the other formats specified above.
Diskopts. diskfilename = "C: \ outputfolder"
'If you do not specify the exact path here (I. e. Including
The drive and directory ),
'Then you wocould find your output file landing up in
'C: \ winnt \ system32 directory-atleast in case of
'Windows 2000 System
Myreport. exportoptions. destinationoptions = diskopts
'The reports export options does not have a filename Property
'That can be directly set. Instead, you will have to use
'The diskfiledestinationoptions object and set its diskfilename
'Property to the file name (including the path) of your choice.
'Then you wocould set the report export options
'Destinationoptions property to point to
'Diskfiledestinationoption object.
Myreport. Export ()
'This statement exports the report based on the previusly set properties.
End sub
Output A report file created by the PUSH model.
when outputting a report file created by the PUSH model, the first step is to manually create a connection and bind the database. Set the 'setdatasource 'Of this reports to this dataset (as described above ). Then you can call the code shown above.