Date post: | 14-Apr-2018 |
Category: |
Documents |
Upload: | vibhor-mittal |
View: | 221 times |
Download: | 0 times |
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 1/10
The following section describes how to create Crystal Report from multipletables in 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
Here we are going to generate Crystal Reports from multiple tables in C#.
Here we have three tables (ordermaster , orderdetails and product ) and weare generating a Crystal Report from these three tables by connecting each
table with their related fields.
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#.
Hope you understand the basics of generating a Crystal Reports in C# , this
section is the continuation of the first part, so here we avoid some basic
steps and start from the table selection of Crystal Reports.
Select all table from the table list to right side list box, because we are
creating report from three tables ( OrderMaster, OrderDetails, Product) . If
you don't know up to this part of the tutorial , refer previous tutorial for upto selecting databese for Crystal reports.
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 2/10
The next step is to make relations between these selected tables. Here we
are connecting the related fields from each table. For that we arrange the
tables in visible area in the list (this is not necessary ) and select the fieldsthat we want to make relation and drag to the related field of the otherselected tables. After made the relations with tables the screen is look like
the following picture .
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 3/10
Next step is to select the fields from the selected tables ( OrderMaster,OrderDetails, Product) . Here we are selecting the fields Customername ,
orderdate from ordermastertable , Productname from product table andquantity from order details table. The field selection screen is look like thefollowing picture .
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 4/10
After select the fields from tables, click the Finish button because now weare not using any other functionalities of the Crystal Reports wizard. After
that you will get the Crystal Reports designer window . You can arrange thefields in the designer window according to your requirement to view thereport .
For re-arranging fields in the designer window , you can drag the field objecton the screen . For editing right click the field object and select Edit Text
Object. The following picture shows the sample of designer window after
rearrange the field.
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 5/10
Now the designing part is over and the next step is to call the CrystalReports in C# and view it in Crystal Reports Viewer control .
Select the default form (Form1.cs) you created in C# and drag a button and
a CrystalReportViewer control to your form .
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 6/10
After you drag the CrystalReportViewer to your form , it will look like thefollowing picture.
You have to include CrystalDecisions.CrystalReports.Engine in your C#
Source Code.
using CrystalDecisions.CrystalReports.Engine;
Copy and paste the following source code and run your C# project
Next : C# Crystal Reports String parameter
Download Source Code
Print Source Code
using System;using System.Windows.Forms;using CrystalDecisions.CrystalReports.Engine;
namespace WindowsApplication1{
public partial class Form1 : Form{
public Form1()
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 7/10
{InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
ReportDocument cryRpt = new ReportDocument();cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");crystalReportViewer1.ReportSource = cryRpt;crystalReportViewer1.Refresh();
}}
}
NOTES:cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project files location, there you can seeCrystalReport1.rpt . So give the full path name of Crystal Reports file like
c:\projects\crystalreports\CrystalReport1.rpt
After you run the source code you will get the report like this.
When you click the button, the application will ask the username and password.
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 8/10
For generating Crystal Reports from C# , we need to connect a database
and some tables with data. In the following section you can see how tocreate a sample Database and Tables and the data for running of the
following Crystal Reports - C# Tutorial . All examples in the CSharpCrystal Reports Tutorial is based on the following database .
First we have to create a database named it as "crystaldb"
Create DataBase "crystaldb"
In the crystaldb database , create three tables
OrderMaster , OrderDetails , Product .
The Table Structure follows :
OrderMasterOrderMaster_id OrderMaster_dateOrderMaster_customer
OrderMaster_createduser
OrderDetailsOrderDetails_id OrderDetails_masterid
OrderDetails_productid
OrderDetails_qty
ProductProduct_id Product_name
Product_price
The following picture shows the relation of tables in crystaldb database :
7/27/2019 Crystal Report From Multiple Tables
http://slidepdf.com/reader/full/crystal-report-from-multiple-tables 9/10
SQL command for creating tables are follows :
CREATE TABLE [dbo].[OrderMaster] ([OrderMaster_id] [int] NOT NULL ,[OrderMaster_date] [datetime] NULL ,[OrderMaster_customername] [varchar] (50),[OrderMaster_createduser] [varchar] (50)
) ON [PRIMARY]
CREATE TABLE [dbo].[OrderDetails] (
[OrderDetails_id] [int] NOT NULL ,[OrderDetails_masterid] [int] NULL ,[OrderDetails_productid] [int] NULL ,[OrderDetails_qty] [int] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Product] ([Product_id] [int] NOT NULL ,[Product_name] [varchar] (50) ,[Product_price] [numeric](18, 0) NULL
) ON [PRIMARY]
Enter some data to the tables :
From the following pictures you can see some data in the table for C# -
Crystal Reports tutorial
Order Master Table Data