Click here to Skip to main content
15,395,257 members
Articles / Programming Languages / C#
Posted 28 Aug 2008


75 bookmarked

Crystal Report with DataSet and DataTable using C#

Rate me:
Please Sign up or sign in to vote.
4.12/5 (44 votes)
8 Sep 2008CPOL2 min read
Crystal Report with Dataset and DataTable using C#


In my previous article, I described how to create Crystal Report with Oracle Views.
In this article, I am going to demonstrate how to create a Crystal Report with ADO.NET DataTable.

You will find this article very interesting, simple and easy to understand.
This article needs basic knowledge of .NET.

Note: Please vote for this article.


No background knowledge is needed for this article. This article is very simple and just requires very basic knowledge of .NET.
But I recommend reading my previous article as well, though it has no direct relation with this article.

Using the Code

Now enough of theory, we will move to our point.
So here we go... I have created 2 sample tables for this project. Scripts of tables are
as follows, with some sample insert query to have sample data.

create table tbl_project

create table tbl_project_group

insert into tbl_project values(1,'CrystalReportWithOracle',1);

insert into tbl_project values(2,'Ajax Application',2);

insert into tbl_project_group values(1,'windows application');

insert into tbl_project_group values(2,'Web application');

First of all, create a project in Microsoft Visual Studio 2005 and name it CrystalReportWithOracle.

Then create a form as frmMain and add a Crystal report viewer control to this form.
Add a Reference to System.Data.OracleClient.

Step 1: Adding A DataSet and DataTable

Add a DataSet to your Project and name it as myDataSet, as follows:


Now add a DataTable to myDataSet:


Now add columns to your DataTable as given below in the image.
Your column name and datatype should be the same as that in your database.


Change the datatype of the DataTable columns as shown below:


Now save this DataTable as my_dt.


Now we have created our DataSet and DataTable. The next step is to create a CrystalReport.

Step 2: Adding A Crystal Report

Add a Crystal report to the project and name it as my_rpt.


Choose Using the report wizard.


Choose the data source from project data which is my_dataset in our case.


Choose the columns to be displayed on the report.


Add a group by column, if you want.


Now our designing part is complete.

Step 3: Binding Our Report to our DataSource

Below is the C# code to bind our report to the datasource.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.IO;

namespace CrystalReportWithOracle
    public partial class frmMain : Form
        public frmMain()

        private void frmMain_Load(object sender, EventArgs e)
            my_rpt objRpt;
            // Creating object of our report.
            objRpt = new my_rpt();

            String ConnStr = "SERVER=mydb;USER ID=user1;PWD=user1";

            OracleConnection myConnection = new OracleConnection(ConnStr);

            String Query1 = "select a.PROJECT_ID,a.PROJECT_NAME,b.GROUP_NAME from 
            tbl_project a,tbl_project_group b where a.group_code= b.group_code";

            OracleDataAdapter adapter = new OracleDataAdapter(Query1, ConnStr);

            DataSet Ds = new DataSet();

            // here my_dt is the name of the DataTable which we 
            // created in the designer view.
            adapter.Fill(Ds, "my_dt");

            if (Ds.Tables[0].Rows.Count == 0)
                MessageBox.Show("No data Found", "CrystalReportWithOracle");

            // Setting data source of our report object

            CrystalDecisions.CrystalReports.Engine.TextObject root;
            root = (CrystalDecisions.CrystalReports.Engine.TextObject)
            root.Text = "Sample Report By Using Data Table!!";

            // Binding the crystalReportViewer with our report object. 
            crystalReportViewer1.ReportSource = objRpt;

Points of Interest

My other articles of interest are:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rehan Ahmad Abbasi
Software Developer (Senior) Al-Jazirah Corporation Riyadh KSA
United Arab Emirates United Arab Emirates
I am a Software Engineer having 4 + years of experience in various skill sets.
Proficiency in,,,, VB 6.0, J2ME, Ajax, Xslt, Xml, Smart Device (Pocket PC 2003), and Oracle.

Extensive experience with analyzing, designing, development, and maintenance of Internet, Intranet, Client Server and Object Oriented applications built on .NET Framework (windows and web app.) and VB 6.0.

Comments and Discussions

QuestionobjRpt.SetDataSource(Ds); Pin
mjocasio2310-Dec-18 14:11
Membermjocasio2310-Dec-18 14:11 
QuestionIt's possible how to show child details on same tab? Pin
Member 107560506-Feb-18 18:06
MemberMember 107560506-Feb-18 18:06 
SuggestionUsing single dataset for designing report and for displaying data to report Pin
PSAFOL30-Dec-12 0:23
MemberPSAFOL30-Dec-12 0:23 
QuestionHi all this is solution Pin
DevNet845-Sep-12 6:23
MemberDevNet845-Sep-12 6:23 
Answerhi Pin
Hani .Yusuf5-Sep-12 6:28
MemberHani .Yusuf5-Sep-12 6:28 
GeneralMy vote of 4 Pin
Dineshnamdev29-Aug-12 22:49
MemberDineshnamdev29-Aug-12 22:49 
QuestionTwo or more tables Pin
solid200523-May-12 23:00
Membersolid200523-May-12 23:00 
QuestionWhat is the "txt_header"? I got a Index Out of range exception in this statement Pin
OlgaGushiken9-Apr-12 12:08
MemberOlgaGushiken9-Apr-12 12:08 
AnswerRe: What is the "txt_header"? I got a Index Out of range exception in this statement Pin
DevNet845-Sep-12 6:22
MemberDevNet845-Sep-12 6:22 
GeneralMy vote of 5 Pin
mrkanepung17-Jan-12 22:35
Membermrkanepung17-Jan-12 22:35 
GeneralMy vote of 5 Pin
aminer1-Jul-11 5:04
professionalaminer1-Jul-11 5:04 
Generaldata not displayed in crytsal report Pin
svknair18-May-11 21:04
Membersvknair18-May-11 21:04 
GeneralMy vote of 5 Pin
balavenkatasivaramkumar7-Dec-10 19:52
Memberbalavenkatasivaramkumar7-Dec-10 19:52 
GeneralMy vote of 3 Pin
shebinbackar5-Aug-10 7:00
Membershebinbackar5-Aug-10 7:00 
GeneralReport without External Dataset Pin
amit.kumar.1311-Nov-09 20:02
Memberamit.kumar.1311-Nov-09 20:02 
GeneralRe: Report without External Dataset Pin
SurajMutha22-Feb-11 16:51
MemberSurajMutha22-Feb-11 16:51 
GeneralDynamically adding columns to datatable Pin
thiru_586210-Jul-09 2:38
Memberthiru_586210-Jul-09 2:38 
GeneralCrystal Reports using data sets and data tables Pin
nitin.mail2021-Apr-09 23:57
Membernitin.mail2021-Apr-09 23:57 
GeneralVery thanks Pin
tarksiala25-Mar-09 17:32
Membertarksiala25-Mar-09 17:32 
GeneralCrystal Report with DataSet and DataTable using C# - responce Pin
Roopangee Tandon22-Oct-08 20:04
MemberRoopangee Tandon22-Oct-08 20:04 
GeneralPlease Vote This Article Pin
Rehan Ahmad Abbasi3-Sep-08 10:45
MemberRehan Ahmad Abbasi3-Sep-08 10:45 
GeneralRe: Please Vote This Article Pin
vinod.chakote6-Sep-10 0:52
Membervinod.chakote6-Sep-10 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.