Crystal Report with C#.Net 2.0
Using Crystal Report is very easy task. Sometime we afraid to use crystal reports with the code of Vb.Net or C#.Net even when we have used crystal reports in the previous technologies like Visual Basic 6. Before writing this tutorial I found some of my friends unable to user crystal report and stuck out in small problems regarding Crystal Report. So I decided to write this tutorial for them and also for those people who are afraid with Crystal Reports and found some difficulties to use it. This tutorial may play a very important role to learn Crystal with Vb.Net/C#.Net for those are new to take crystal with .Net but have used crystal with some of the earlier technologies or who are new even to Crystal.
So Friends, Lets Have a look on the tools used in crystal reports with VB.Net/C#.Net.
1) DataSet (System.Data.DataSet)
2) Crystal Report
3) CrystalReportViewer1 (CrystalDecisions.Windows.Forms.CrystalReportViewer)
4) System.Data.SqlClient
Now Follow these Steps for the first stage to learn the crystal reports
Stage 1 Create Data Base Table (I ussed SqlServer2000)
1. Create These Basic Tables using SqlServer2000
Create Table DemoStudentMaster
(
StudentId int primary key,
StudentName varchar(15),
StudentAddress varchar(50),
StudentCity varchar(20),
StudentMobile varchar(10)
)
Create Table StudentFeeDetails
(
StudentId int references DemoStudentMaster(StudentId),
SumittedFeeAmt int,
ForMonth int,
ForYear int
)
2. Insert Values in These Tables
DemoStudentMaster
insert into DemoStudentMaster values (1,’Vinay Hatwal’,'Nehru Place’,'New Delhi’,'9911884523′);
insert into DemoStudentMaster values (2,’Panjak Pundir’,'Nehru Place’,'New Delhi’,'7483434356′);
insert into DemoStudentMaster values (3,’Ashish C.’,'Nehru Place’,'New Delhi’,'7473949866′);
insert into DemoStudentMaster values (4,’Sandeep S.’,'Nehru Place’,'New Delhi’,'791198475′);
insert into DemoStudentMaster values (5,’Pradeep K.’,'Nehru Place’,'New Delhi’,'8911884523′);
StudentFeeDetails
insert into StudentFeeDetails values (1,1000,2,2008);
insert into StudentFeeDetails values (1,1000,3,2008);
insert into StudentFeeDetails values (1,1000,4,2008);
insert into StudentFeeDetails values (1,1000,5,2008);
insert into StudentFeeDetails values (1,1000,6,2008);
insert into StudentFeeDetails values (1,1000,7,2008);
insert into StudentFeeDetails values (2,4000,2,2008);
insert into StudentFeeDetails values (2,4000,3,2008);
insert into StudentFeeDetails values (2,4000,4,2008);
insert into StudentFeeDetails values (2,4000,5,2008);
insert into StudentFeeDetails values (2,4000,6,2008);
insert into StudentFeeDetails values (3,3000,2,2008);
insert into StudentFeeDetails values (3,3000,3,2008);
insert into StudentFeeDetails values (3,3000,4,2008);
insert into StudentFeeDetails values (5,10000,2,2008);
insert into StudentFeeDetails values (5,10000,3,2008);
Now Your Tables Are Ready To Use in Crystal Report.
Stage 2 – Creating Sample Application in .Net
- Using C#.Net 2.0
a) Open a new Project with C# names MyDemoCrystalPrj.
b) Open Solution Explorer , Right Click on MyDemoCrystalPrj then click on Add and then click on New Items.
c) Now Select The DataSet. Renamed it as SampleDataSet , Click on Add Button. Now The SampleDataSet.xsd tab will display.
d) Right click on the middle of the page and then click on Add and then DataTable, After Clicking on it a Data Table will Appear on screen.
e) Now Right Click on DataTable1 and select Rename . Rename id as StudentMasterTable.
f) After it Right Click on The table and Select Add then Column. Make The Following Column in it.
a. StudentID
b. StudentName
c. StudentAddress
d. StuentMob
g) Now Again Open Solution Explorer , Right Click on MyDemoCrystalPrj then click on Add and then click on New Items then select Crystal Report. Rename it as MasterCrystalReport.rpt(All the related references will be automatically added in the references)
h) After Adding Crystal In Project Following Dialog Box Will appear on the screen
i) Select option As a Blank Report and then press OK. Crystal Report will appear and looks like
j) Now right click on DataBase Fields in the Field Explorer , Select Database Expert. After selecting it following screen will appear.
Select Project Data > ADO.NET DataSets > MyDemoCrystalPrj.SampleDataSet
then add it and then click on OK
k) Now Field Explorer will look like
l) Drag then Fields of StudentMasterTable in the detail section of the Crystal Report. You Can also take the heading on the Report Header Section .Right Client On Report Header Section , Select Insert then Select Text Object. Give the Text as Student Detail and format it according to your need. After Inserting all the things your report will look like
m) Now Open Form1 and Take CrystalReportViewer Form the Crystal Report Strip in the Toolbox.
n) Now Go To the Coding Window of Form1 and write the following code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyDemoCrystalPrj
{
public partial class Form1 : Form
{
SqlConnection con;
DataSet ds;
public Form1()
{
SqlDataAdapter adp;
con = new SqlConnection(“Server=.;Database=master;Integrated Security=true”);
ds = new DataSet();
adp = new SqlDataAdapter(“select StudentId,StudentName,StudentAddress,StudentCity,StudentMobile from DemoStudentMaster”, con);
adp.Fill(ds, “StudentTable”);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MasterCrystalReport cr = new MasterCrystalReport();
cr.SetDataSource(ds.Tables["StudentTable"].DefaultView);
crystalReportViewer1.ReportSource = cr;
crystalReportViewer1.Show();
}
}
}
o) Finally Run The Project. Your Crystal Report Will Look Like
Note – If you find the helps you for learning the crystal report as a beginner the please write comment on this tutorial or just writ few words on vinayhatwal@gmail.com. I will be back by taking some new tutorial on new technologies soon.









