C # Crystal Report tutorial

Source: Internet
Author: User
Tags microsoft sql server 2005

http://apps.hi.baidu.com/share/detail/24298108

Crystal Reports is a powerful reporting tool that has now been integrated with Microsoft Visual Studio 2005 (hereinafter referred to as VS2005). Friends who like Crystal Reports are easy to use. I vs2005 the Crystal report in the use of the method summed up for your reference.
First, let me introduce the SOFTWARE environment I use: Microsoft Visual Studio 2005;microsoft SQL Server 2005
"Data use case"
Server: SQLEXPRESS
Database name: Test
database table: T

"description"
The Crystal Report is applied at the time of the two methods, namely pull mode, push mode (push). Pull mode: The data source at the time of the Crystal Report generation is extracted from the database from the SQL statement in the Crystal Report file, without rewriting the SQL statement while programming, but with the login information (as described later). Push mode: The data source at the time of Crystal Report generation is the dataset pair image generated by rewriting the SQL statements in the Crystal report when programming. In other words, the push mode is to assemble the Crystal Report with a dataset. The
Crystal Report components are described. The Crystal Report has two components in VS2005, and the Web project is Crystalreportsource,crystalreportviewer respectively. In the form project, the distinction is crystalreport,crystalreportviewer. The
Crystalreportsource,crystalreport is the data provider for Crystal Reports; Crystalreportviewer is the browser for Crystal Reports. Also to introduce the water report file is an rpt extension file, which can be generated with VS2005. The
below describes how to do this:
Pull mode:
in pull mode if you want to add a conditional parameter to the SQL statement in the Crystal Report, use the {? The parameter name} mode is given. Example: "Select T1, T2, T3 from T Where t1= ' {? Parm} '" Parm is the name of the parameter

The SQL statement used in the Crystal Report file that is used in the following example is "Select T1, T2, T3 from T Where t1= ' {? Parm} '" Parm is the name of the parameter.
"Web Mode"
using Crystaldecisions.shared;
Using CrystalDecisions.CrystalReports.Engine;
   ///<summary>
   //feature: Pull mode extract Crystal Report
  ///Personal home page: http://www.dzend.com/
   ///</summary>
   ///<param Name= "Sender" ></PARAM>
   ///<param name= "E" ></PARAM>
     protected void Button_pull_click (object sender, EventArgs e)
{
// Crystalreport.rpt is the name of the Crystal Report file; CrystalReportSource1 is a crystal report data source pair image that is added from the toolbox to the page.

        CrystalReportSource1.ReportDocument.Load (Server.MapPath (" Crystalreport.rpt ")); The
///Setdatabaselogon mode must use this method to set the login information, parameter one: User name, parameter two: password, parameter three: server; parameter four: database name
         CrystalReportSource1.ReportDocument.SetDatabaseLogon ("sa", "123456", @ "sywzswl\sqlexpress", "Test");
//To the Crystal Report parameters, parameter one: is the parameter name, parameter two: parameter value;
        CrystalReportSource1.ReportDocument.SetParameterValue ("Title", "This is a test report");
        CrystalReportSource1.ReportDocument.SetParameterValue ("Parm", "1" );
//Binding Crystal Report data source.
        Crystalreportsource1.databind ();
//CrystalReportViewer1 is a Crystal report browser, the following is to assign the browser to the image
        Crystalreportviewer1.reportsource = CrystalReportSource1;
        crystalreportviewer1.databind ();

} "Form Mode"
In the form of the code with the Web way, with the Crystalreport control to replace the Crystalreportsource; replaced Crystalreportviewer with Crystalreportviewer. Both of these controls can be found in the toolbox. Also remove the DataBind () method when programming.
private void Form1_Load (object sender, EventArgs e)
{

Crystalreport1.load (Application.startuppath + "crystalreport.rpt");

Crystalreport1.setdatabaselogon ("sa", "123456", @ "sywzswl\sqlexpress", "Test");

Crystalreport1.setparametervalue ("Title", "This is a test report");
Crystalreport1.setparametervalue ("Parm", "1");
Crystalreportviewer1.reportsource = CrystalReport1;

}


Push mode (push):
The fields in the SQL statement in the dataset programmed in the push mode are consistent with the SQL statement fields in the Crystal Report. To put it simply, the Crystal report in the push mode is a template that sets the format of the report in the designer and then assembles the dataset to generate the report.


"Web Mode"

Using Crystaldecisions.shared;
Using CrystalDecisions.CrystalReports.Engine;
Using System.Data.SqlClient;
protected void Button_push_click (object sender, EventArgs e)
{
String sql = "Select T1, T2, T3 from T where t1= ' a '";
String Dbconfig_sql [email protected]"DataSource=sywzswl\sqlexpress;initial catalog=test; User Id=sa; password=123456 ";
DataSet ds = new DataSet ();
SqlConnection Sqlcon = new SqlConnection (dbconfig_sql);
SqlCommand SQLCMD = new SqlCommand (sql, Sqlcon);
SqlDataAdapter sqlad = new SqlDataAdapter ();
Sqlad.selectcommand = SQLCMD;
Sqlad.fill (ds, "SQL");
CrystalReportSource1.ReportDocument.Load (Server.MapPath ("crystalreport.rpt"));
Note You must indicate the name of the table in the dataset, or you will be prompted to "require more information for the report you requested."
CrystalReportSource1.ReportDocument.SetDataSource (ds. tables["SQL"]);
//{?} The parameters in the can not be assigned, even if the value is assigned does not work.
crystalreportsource1.reportdocument.parameterfields["Parm"]. Currentvalues.addvalue ("1234567");
crystalreportsource1.reportdocument.parameterfields["Title"]. Currentvalues.addvalue ("Then push the Model report sample! ");
Crystalreportsource1.databind ();

Crystalreportviewer1.reportsource = CrystalReportSource1;
Crystalreportviewer1.databind ();
}
"Form Mode"
private void Form1_Load (object sender, EventArgs e)
{
Push mode
String sql = "Select T1, T2, T3 from T where t1= ' a '";
String dbconfig_sql = @ "Data source=sywzswl\sqlexpress;initial catalog=test; User Id=sa; password=123456 ";
DataSet ds = new DataSet ();
SqlConnection Sqlcon = new SqlConnection (dbconfig_sql);
SqlCommand SQLCMD = new SqlCommand (sql, Sqlcon);
SqlDataAdapter sqlad = new SqlDataAdapter ();
Sqlad.selectcommand = SQLCMD;
Sqlad.fill (ds, "SQL");
Crystalreport1.load (Application.startuppath + "crystalreport.rpt");
Crystalreport1.setdatasource (ds. tables["SQL"]);
//{?} The parameters in the can not be assigned, even if the value is assigned does not work.
crystalreportsource1.reportdocument.parameterfields["Parm"]. Currentvalues.addvalue ("1234567");
crystalreport1.parameterfields["Title"]. Currentvalues.addvalue ("Then push the Model report sample! ");

Crystalreportviewer1.reportsource = CrystalReport1;
}


--------------------------------------------------------------------------------------------------------------- ---------------------------------------------


How to use Crystal Report programming

1. How to make the group header appear on each page in the same group
When inserting a group, on the Common Options page, select Repeat group header on each page
2. How to pass parameters to Crystalreportviewer and reportdocument ...
First, Crystalreportviewer
Through the Crystalreportviewer.parameterfieldinfo property ...
1. Crystalreportviewer Members
Public instance Properties
Parameterfieldinfo (inherited from Crystalreportviewerbase) parameterfields. Gets or sets the parameter field collection.
Second, reportdocument
Through the ReportDocument.DataDefinition.ParameterFields property ...
1. Reportdocument Members
Public instance Properties
Datadefinition datadefinition. Gets the Datadefinition object.
2. Datadefinition Members
Public instance Properties
Parameterfields parameterfielddefinitions. Gets the Parameterfielddefinitions collection.
Third, parameter field runtime customization (crystalreportviewer)
Reference:
Setting Parameters | Binding options for Windows Forms Viewer | Binding options for the Web forms viewer
Enables users to enter through parameters in a Crystal report. This parameter has many uses. For example:
Causes the parameter to be based on a database field and allows the user to specify the value of the field to filter the data in the report.
Use parameter fields to apply conditional formatting to a report.
Use the parameter fields to define the sort order.
The following example shows how to set parameter field values at run time through code. This example explains how to set two different parameters: the first is a multivalued discrete parameter, and the second is a zone value parameter.
modifying parameter fields at run time
[C #]
Declaration to pass parameters to the
The variable required by the viewer control.
Parameterfields paramfields = new Parameterfields ();
Parameterfield Paramfield = new Parameterfield ();
Parameterdiscretevalue discreteval = new Parameterdiscretevalue ();
Parameterrangevalue rangeval = new Parameterrangevalue ();
The first parameter is a discrete parameter with multiple values.
Sets the name of the parameter field, which must be
Matches the parameters in the report.
Paramfield.parameterfieldname = "Customer Name";
Sets the first discrete value and passes it to the parameter.
Discreteval.value = "AIC childrens";
PARAMFIELD.CURRENTVALUES.ADD (Discreteval);
Sets a second discrete value and passes it to the parameter.
The Discreteval variable is set to the new value, so that the previous setting
will not be overwritten.
Discreteval = new Parameterdiscretevalue ();
Discreteval.value = "Aruba Sport";
PARAMFIELD.CURRENTVALUES.ADD (Discreteval);
Adds the parameter to the Parameter field collection.
Paramfields.add (Paramfield);
The second parameter is a range value. Paramfield variable
is set to the new value so that the previous settings are not overwritten.
Paramfield = new Parameterfield ();
Sets the name of the parameter field, which must be
Matches the parameters in the report.
Paramfield.parameterfieldname = "Customer ID";
Sets the start and end values of the range and passes the range to the
The parameter.
Rangeval.startvalue = 42;
Rangeval.endvalue = 72;
PARAMFIELD.CURRENTVALUES.ADD (Rangeval);
Adds a second parameter to the Parameter field collection.
Paramfields.add (Paramfield);
Places a collection of parameter fields into the viewer control.
Crystalreportviewer1.parameterfieldinfo = Paramfields;
For more information, please see:http://www.devedu.com/develop/2005-4-8/12305/default.aspx
3. Setting the appearance of the Crystal Report Viewer
To set the properties of the Crystal report Viewer:
The Bestfitpage boolean value. Gets or sets whether the page view is the right size or is clipped with a scroll bar.
When this place is set to false, the width of the Crystal Report Viewer can be used to remove the scroll bar.
Description: There are currently two scenarios in which the problem occurs when printing from the Web Forms viewer:
The Bestfitpage property is the default value of true (that is, there is no vertical or horizontal scrollbar), but Pagezoomfactor is greater than 100.
Bestfitpage is set to False, the height of the Web form Viewer is less than the height of the report page (that is, there is a vertical scrollbar), and the width of the viewer is greater than or equal to the width of the report page (that is, there is no horizontal scroll bar).
With set Width, Height to achieve no blank and no scroll bar display!
The Displaygrouptree boolean value. Gets or sets whether the tree view is visible or hidden.
The Displaypage boolean value. Gets or sets whether the toolbar is visible or hidden.
The Displaytoolbar boolean value. Gets or sets whether the go to Page button on the toolbar is visible or hidden.
Pagezoomfactor Int32. Gets or sets the scale factor for the report.

The Separatepages boolean value. Gets or sets whether the report page is separate or connected.
Pagetotreeratio Float64. Sets the size ratio between the group tree and the report view.
4. Control the toolbar buttons:
To set the properties of the Crystal report Viewer:
The Hasgotopagebutton boolean value. Gets or sets the visibility of the go to page button.
The Haslevelupbutton boolean value. Gets or sets whether the go to Previous page button on the toolbar is visible or hidden.
The Haspagenavigationbuttons boolean value. Gets or sets whether the page navigation buttons on the toolbar are visible or hidden.
The Hasrefreshbutton boolean value. Gets or sets whether the Refresh button on the toolbar is visible or hidden.
The Hassearchbutton boolean value. Gets or sets whether the Search button on the toolbar is visible or hidden.
The Haszoomfactorlist boolean value. Gets or sets whether the list of scale factors on the toolbar is visible or hidden.
5. Layout in the Web page:
In Design view, modify the Width, Height property of the Crystalreportviewer.
Switch to the HTML view of the form to modify the Style property.
<cr:crystalreportviewer id= "CrystalReportViewer1" style= "Z-INDEX:101; left:8px; Position:absolute; top:8px "runat=" Server "width=" 350px "height=" 50px "enabledrilldown=" false "Displaygrouptree=" false "&GT;&LT;/CR: Crystalreportviewer>
6. Company logo In addition to Crystal Report
Replace or delete the logo file of Crystal company ...
(1) If you are using a Crystal report from Vs.net
C:\Program Files\Microsoft Visual Studio Net\crystal Reports\viewers\images\toolbar\logo.gif
(2) If it is with Crystal Report 9.2
C:\Program Files\Common Files\Crystal Decisions\2.0\crystalreportviewers\images\toolbar\crlogo.gif
7. Replace the various icons and pictures in the Crystal Report
(1) If you are using a Crystal report from Vs.net
C:\Program Files\Microsoft Visual Studio Net\crystal Reports\viewers\images
(2) If it is with Crystal Report 9.2
C:\Program Files\Common Files\Crystal Decisions\2.0\crystalreportviewers\images
8. Install the plugin when printing:
In the newer version of Crystal Reports, you can use the new Crystalreportviewer.printmode property to specify the print mode. It contains two options: ActiveX and PDF.
When the property is set to Printmode.pdf, the report is exported to PDF on the Web server and then streamed to the browser, where the user can print directly to the printer using the options. This option is cross-platform compatible.
When the property is set to Printmode.activex, the ActiveX print control entry allows the consumer to print the report directly to a local printer

Download FirstHttp://support.businessobjects.com/CRforVS2005/PrintControl.cab, and put it on your own server.

To display the CAB package on a Crystal Reports for Visual Studio 2005 Web site, add the following XML statement to the Web. config file for the site (must be added to <configuration xmlns= "http://schemas.microsoft.com/.NetConfiguration/v2.0">, which is the front):
<configSections>
<sectiongroup name= "BusinessObjects" >
<sectiongroup name= "Crystalreports" >
<section name= "Printcontrol" type= "System.Configuration.NameValueSectionHandler, System, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089, Custom=null "/>
</sectionGroup>
</sectionGroup>
</configSections>
<businessObjects>
<crystalReports>
<printControl>
<add key= "url" value= "Http://192.168.88.91/PrintControl.cab "/>---here instead. Address where the CAB package resides
</printControl>
</crystalReports>
</businessObjects>
Note: ActiveX mode is supported only by Internet Explorer. If you print from a browser other than Internet Explorer (FireFox, Safari, Mozilla, etc.), it will revert to the PDF Pop-up dialog box.

Then add a layer in the printed page, or directly in the main form, and add the following code to the layer (other ways can be used)
<object id= "Crystalprintcontrol" classid= "clsid:baee131d-290a-4541-a50a-8936f159563a"
Codebase= "Http://192.168.88.91/printcontrol.cab"height=" 0px "version=" 10,2,0,1078 "
viewastext= "" width= "0px" >
</object>
Version is the revision number, and if you are the other version, modify the version number.
9. The maximum number of report processing jobs configured by the system administrator has been reached
Workaround:
Specifically, modify the values for the following two keys.
HKEY_LOCAL_MACHINE; Software; Crystal decisions;10.0; Report Application
Server;inprocserver; Printjoblimit modified to 1000
There is also a HKEY_LOCAL_MACHINE; Software; Crystal decisions;10.0; Report Application
Server; Server; Printjoblimit also changed to 1000
I found that in C:\WINDOWS\Temp this temporary file contains a large number of Crystal report files, each use will generate several files, the computer does not restart the case it will not be deleted, and there are a lot of useless files, Search in Google in a circle found that some people encounter such a situation but the answer is very vague only to say that the Crystal Report loading documents to close off, from here can be seen that such errors should be the programmer.
The specific solution is as follows:
1.ReportDocumen instance must be a class member
Private Reportdocument PRTP = new Reportdocument ();
2. After using the Crystal Report must close the file, this will not be generated in the Windows temporary files.
private void Page_Unload (object sender, EventArgs e)
{
PRTP. Dispose ();
}
The Page_Unload event is run when the page is fully displayed, which is resolved.

This article comes from (http://hi.baidu.com/%D2%AA%C4%E3%C3%FC%C8%FD%C7%A7%D6%AE%D5%D4/blog/item/ ba3eaecefd192c34f8dc6189.html)

From: http://hi.baidu.com/xzyweb/blog/item/a70f9aa59f39e59ed1435827.html

C # Crystal Report tutorial

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.