Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.18 MB, 71 trang )
Createting two date parameters (dateFrom and dateTo).
Date From parameter.
Date To Parameter
Here we have three tables selected for report (ordermaster , orderdetails and
product ) and we are making the formula like , select all records from the tables
whose order date is between fromDate parameter and toDate paramater . For
doing this you have to select from date as Ordermaster.orderdate, to date as
Ordermaster.orderdate , comparison operators , boolean operator (AND) and
date parameters fromdate and todate fields from selection list of Formula Editor
and make the formula.
The following picture shows how to select the fields from formula editor and
make the formula. Double click each field then it will automatically selected .
After the creation of selection formula you can close that screen .
Now the designing part is over and the next step is to call the Crystal Reports in
C# and view it in Crystal Reports Viewer control .
Select the default form (Form1.cs) you created in CSharp and drag two
Textboxs (from date entry and to date entry) , a button and
CrystalReportViewer control to your form.
Copy and paste the following source code and run your C# project
using
using
using
using
System;
System.Windows.Forms;
CrystalDecisions.CrystalReports.Engine;
CrystalDecisions.Shared;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("PUT CRYSTAL REPORT PATH
HERE\CrystalReport1.rpt");
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;
ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new
ParameterDiscreteValue();
crParameterDiscreteValue.Value = textBox1.Text;
crParameterFieldDefinitions =
cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition =
crParameterFieldDefinitions["fromDate"];
crParameterValues =
crParameterFieldDefinition.CurrentValues;
crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterVal
ues);
crParameterDiscreteValue.Value = textBox2.Text;
crParameterFieldDefinitions =
cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition =
crParameterFieldDefinitions["toDate"];
crParameterValues =
crParameterFieldDefinition.CurrentValues;
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterVal
ues);
crConnectionInfo.ServerName = "YOUR SERVERNAME";
crConnectionInfo.DatabaseName = "DATABASE NAME";
crConnectionInfo.UserID = "USERID";
crConnectionInfo.Password = "PASSWORD";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table
CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
}
}
}
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your project files location, there you can see
CrystalReport1.rpt . So give the full path name of Crystal Reports file like
c:\projects\crystalreports\CrystalReport1.rpt
crConnectionInfo.ServerName = "YOUR SERVERNAME";
crConnectionInfo.DatabaseName = "DATABASE NAME";
crConnectionInfo.UserID = "USERID";
crConnectionInfo.Password = "PASSWORD";
You have to pass the necessary database connection information.
Now you can run the program . Enter from date and to date , then you can see
the report whose order date is between from date and to date you passed to
Crystal reports.
19. How to print Crystal Reports in C#
The following program shows how to print multiple copies of Crystal reports
from C#.
All C# Crystal Reports Tutorial in this website is based on the following
database - crystaldb. So before you begin this section , please take a look at the
database structure of crystaldb - Click Here C# crystaldb
If you are new to Crystal Reports and do not know how to create Crystal
Reports from C# , please take a look at the section step by step tutorial for
creating a Crystal Reports from C#.
The following program generate a Crystal Reports from product table and print
2 copies of report without using CrystalReportViewer in C#. The program
generate report from Product table , the step by step tutorial for creating a
Crystal Reports from C# section explains how to create report from Product
table. Also the program dynamically passing the logon parameters to Crystal
Report. If you don't know how to pass dynamic logon parameter please take a
look at the section C# Dynamic logon parameters in Crystal Reports.
Hope you understand the previous section well. Create a Crystal report from
Product Table and copy and paste the following source code in the project.
Copy and paste the following source code and run your C# project
using
using
using
using
System;
System.Windows.Forms;
CrystalDecisions.CrystalReports.Engine;
CrystalDecisions.Shared;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("PUT CRYSTAL REPORT PATH
HERE\CrystalReport1.rpt");
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;
crConnectionInfo.ServerName = "YOUR SERVERNAME";
crConnectionInfo.DatabaseName = "DATABASE NAME";
crConnectionInfo.UserID = "USERID";
crConnectionInfo.Password = "PASSWORD";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table
CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
cryRpt.Refresh();
cryRpt.PrintToPrinter(2, true, 1, 2);
}
}
}
cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your project files location, there you can see
CrystalReport1.rpt . So give the full path name of Crystal Reports file like
c:\projects\crystalreports\CrystalReport1.rpt
crConnectionInfo.ServerName = "YOUR SERVERNAME";
crConnectionInfo.DatabaseName = "DATABASE NAME";
crConnectionInfo.UserID = "USERID";
crConnectionInfo.Password = "PASSWORD";
You have to pass the necessary database connection information.
Now you can run the program . When you run this program the program will
print multiple copies of Crystal Reports.
20. How to deploy Crystal Report
The following section describes how to deploy a Crystal Reports components
and assemblies on the target machine.
If you are new to Crystal Reports and do not know how to create Crystal
Reports from C# , please take a look at the section step by step tutorial for
creating a Crystal Reports from C#.
Crystal Reports for Visual Studio ships with your deployment projects that
enable you to deploy Crystal Reports components and assemblies on the target
machine. We can use different approaches to install Crystal Reports runtime
files on the target machine in order to run your C# applications smoothly.
If you are using Visual Studio then during your setup and deployment you can
add the CRRedist2005_x86.msi file to your setup file and distribute it as a
single setup file with your C# application. The installer can execute the setup
file automatically within your .Net Project in a single click. You can find the
CRRedist2005_x86.msi file in your system's C:\Program Files\Microsoft
Visual Studio 8\SDK\v2.0\BootStrapper\Packages\CrystalReports . Also you
can distribute the CRRedist2005_x86.msi separately and install it on the target
machine where your C# applications installed.
The other way to install Crystal Reports on target machine is to create a setup
file using Merge Modules and distribute it with your C# application or as a
separate setup file. Click the following link to see how to make a setup file with
Merge Modules.
21. How to Crystal Report Merge Module
The following section shows how to create a setup file for Crystal Reports
Client side installations using Merge Modules.
If you are new to Crystal Reports and do not know how to create Crystal
Reports from C# , please take a look at the section step by step tutorial for
creating a Crystal Reports from C#.
In the previous section How to install C# Crystal Reports on Clinet machine ,
we already saw how to deploy a Crystal Reports components and assemblies on
the target machine using CRRedist2005_x86.msi file. Here we are creating a
separate setup file for client side installations using Crystal Report Merge
Modules.
Crystal Reports uses merge modules to ensure the correct report components
and assemblies are installed with your deployment of C# project. A merge
module is a set of components that are merged with a Windows installer for
applications that need them. The following steps are creating a setup file for
Crystal Reports Client side installation using Merge Modules.
Before creating a setup project, make sure that the following files exist in the
\Program Files\Common Files\Merge Modules folder.
CrystalReportsRedist2005_x86.msm
Microsoft_VC80_ATL_x86.msm
policy_8_0_Microsoft_VC80_ATL_x86.msm
Steps to create Setup file for your C# Application.
1) Make sure the above mentioned files are in the \Program Files\Common
Files\Merge Modules folder , If the files are not there you can download the file
and copy it to the folder \Program Files\Common Files\Merge Modules .
Download: https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/11688
2) Open your Visual Studio
3) Select "New Project" from File menu.
4) Select "Setup and Deployment" from "Other Project Types"
5) In the "Solution Explorer", select your setup project, right-click, and select
Add and Merge Module from the menu.
6) Select CrystalReportsRedist2005_x86.msm to your setup project.
The file available at \Program Files\Common Files\Merge Modules
Then you can see Microsoft_VC80_ATL_x86.msm and
policy_8_0_Microsoft_VC80_ATL_x86.msm will be automatically added in
the Detected Decencies section.
7) Build your project.
Now you will get the setup file for distribution. Either you can add this setup
file to your C# application installer or distribute it as a separate setup file.