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 )
section explains how to avoid the dialogue box asking username and password
at runtime on Crystal Reports login . In this section we are passing User ID ,
Password , Server Name and Database Name dynamically to 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#.
In many situations dynamically passing the logon parameter fields are very
useful , for example if you develop a database project in a test server
environment and later you have to migrate it to other server , in these situations
it is better to give these information dynamically to Crystal Reports .
In step by step tutorial for creating a Crystal Reports from C# - we created a
report selecting all data from the Product table . There is no chance to change
the server name or any other information on runtime in that case, because it is a
static report and it will asks username and password every time you run the
Crystal Reports . Here we are passing Server Name , UserID and Password
dynamically to the Crystal Reports from our C# program
Here we use Crystal Reports ConnectionInfo class for passing connection
information.
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Select the default form (Form1.cs) you created in C# and drag a button and a
CrystalReportViewer control to your form .
You have to include CrystalDecisions.CrystalReports.Engine in your C#
Source Code.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
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();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables ;
cryRpt.Load("PUT CRYSTAL REPORT PATH
HERE\CrystalReport1.rpt");
crConnectionInfo.ServerName = "YOUR SERVER NAME";
crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
crConnectionInfo.Password = "YOUR DATABASE 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 C# 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
You have to replace the source code values of
crConnectionInfo.ServerName = "YOUR SERVER NAME";
crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
crConnectionInfo.Password = "YOUR DATABASE PASSWORD";
with your real-time values .
8. C# Crystal Reports Formula Field
The following C# - Crystal Reports section describes how to add a formula
field in the Crystal Reports .
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 situations when formula field is using :
If you have a Crystal Reports with Qty and Price fields and you need an
additional field in your Crystal Reports for Total, that is TOTAL = QTY X
PRICE . In these types of situations you can use the Formula Field in Crystal
Reports.
In this C# Crystal Reports tutorial we are showing that all orders with qty and
price and the total of each row , that means each in each row we are showing
the total of qty and price.
Before starting this tutorial Create a new Crystal Reports with fields
CustomerName , Order Date , Product Name and Product Price . If you do not
know how to create this report , just look the previous tutorial C# Crystal
Reports from multiple tables . In that report selecting only four fields , here we
need one more field Product->Price and one formula field Total.
After you create the above Crystal Reports, your CR designer screen is look
like the following picture :
Next step is to create a Formula Field for showing the result of Qty X Price .
Right Click the Formula Field in the Field Explorer and click New. Then you
will get an Input Message Box , type Total in textbox and click Use Editor.
Now you can see the Formula Editor screen . Here you can enter which formula
you want . Here we want the result of Qty X Price . For that we select