Click here to Skip to main content
Click here to Skip to main content

Creating Crystal Reports using C# with Datasets

By , 13 Jan 2006
 
Sample screenshot

Introduction

This article is about creating Crystal Reports using C#.NET. About two weeks ago, one of my friends asked me for this kind of example. He also searched through the Internet and said that he couldn't find a sample. When I was doing this kind of development, I also looked for samples, but I couldn't find one. That’s why I wrote this article on The Code Project without creating reports separately using Crystal Reports other than using .NET IDE.

Create a Dataset

First you should create a Dataset to get data from the DB. By clicking the “Add New Item” in the “Project” menu, you can add a Dataset. A picture of an added Dataset looks like the following window and you can add elements to the Dataset by dragging and dropping elements from the toolbox. This Dataset is used to fill the data, which it gets from the DB by using a query. Because of that, the names of the elements in this Dataset should be equal to the names of the elements in the DB. For an example, this Dataset can fill using the following query.

String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_
	Data Source=..\\..\\myDB.mdb;User ID=Admin;Password=";

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string query = "SELECT studentID, firstName, lastName, birthDate, _
				address, contactNo FROM studentInfo";
OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);
Sample screenshot

Create a Report using Crystal Reports

Now that we have a created Dataset, we can use it to fill the report with data, which we will be getting from the DB. As I mentioned before, you can add a Crystal Report to the project by clicking “Add New Item” in the “Project” menu. Then the following window will appear, and you can select your choices and click OK.

Sample screenshot

The next window looks like the following and you have to select your created Dataset under “Project Data”, and click “Insert Table”, then click next.

Sample screenshot

Then you have to add the field, which you want to display in the report through the following window and click next.

Sample screenshot

You can also go to other tabs of this window and select/deselect your choices. Use the last tab “Style” to select the format of the report. You can also type a Report Title here and click finish.

Sample screenshot

Then your report creation is done by the .NET IDE. If you want to make any changes to the report, you can do so by using the .NET IDE.

Set the Created Report to Display in the Form

Then you have to set a “crystalReportViewer” in your form to load the report that you created earlier. Moreover, you need to set the report source of this “crystalReportViewer” component, which falls in the properties panel or you can set the report source by using the code like the following:

// code to get data from the DB
DBConnection DBConn = new DBConnection();
OleDbDataAdapter myDataAdapter = DBConn.getDataFromDB();

// use the created Dataset to fill it with data got
// from the DB by using the DataAdapter
DataSet dataReport = new DataSet();
myDataAdapter.Fill(dataReport,"myPersonalInfoTable");
// create a new report from the created CrystalReport
myDataReport myDataReport = new myDataReport();

// set the data source of the report
myDataReport.SetDataSource(dataReport);

// set the report source of the created “crystalReportViewer”
// component to the created report
crystalReportViewer1.ReportSource = myDataReport;

Additional Information

You can also create reports using Crystal Reports separately without using the .NET IDE. For that, you have to install Crystal Reports as well, and you should save those reports in a directory. Then you have to set the report source of the “crystalReportViewer” component to the particular report under your report Directory. For example:

crystalReportViewer1.ReportSource = @“..\Reports\salesReport.rpt”;

But a better way is the previous one, because we can get data from the DB according to the inputs, which are done by the user of the Applications.

Regarding this article, you can send any questions to isharasjc@gmail.com. I will send you any information that you want for your development.

History

  • 13th January, 2006: Initial post

License

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

About the Author

Ishara Gunarathna
Web Developer
Sri Lanka Sri Lanka
Member
I'm Ishara Gunarathna, working as a Software Engineer. If you meet me someday, somewhere say hi to me.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCrystal Reportmemberntitish5 Mar '13 - 21:24 
QuestionHow Print Customer Recepiet???memberkashifjaat29 Nov '12 - 16:45 
GeneralMy vote of 5memberChathura Roshan1 Nov '12 - 20:05 
QuestionC# .netmemberajeet19886 Sep '12 - 21:17 
QuestionThanksmemberYasir Zeshan16 Jul '12 - 1:54 
QuestionThanksmemberSoheera19 Jun '12 - 0:58 
Questioncrystal reportmemberdevmurari hitarthi16 Apr '12 - 20:50 
Questionc#.netmemberdevmurari hitarthi16 Apr '12 - 19:43 
GeneralMy vote of 5membermanoj kumar choubey30 Mar '12 - 3:21 
GeneralMy vote of 2membersravan_34128 Mar '12 - 0:07 
GeneralMy vote of 5membernitinpandit21 Mar '12 - 21:04 
GeneralCrystal reports from data gridview???memberChathurange17 Jun '11 - 6:55 
Questioncreating crystal reportsmember080702728 Apr '11 - 4:16 
GeneralSetting page header dynamically in crystal reportmemberMember 401433910 Aug '10 - 16:58 
GeneralGetDataFromDB methodmemberyandroide22 Apr '10 - 6:32 
QuestionWhere did myDataReport class come from?membercrystalriver27 Feb '10 - 4:07 
AnswerRe: Where did myDataReport class come from?memberThesisus28 Apr '10 - 5:59 
GeneralGood job dude !memberbalaji_ram25 Feb '10 - 3:10 
GeneralTitle Setmembereusufkhan21 Feb '10 - 21:36 
GeneralRe: Title SetmemberNirosh20 Apr '11 - 22:21 
GeneralcoolmemberMico_Perez_II11 Feb '10 - 21:26 
Generalgood work.., nice jobmemberamilakandambi4 Feb '10 - 6:03 
GeneralGood JobmemberArielR8 Jan '10 - 13:06 
QuestioncrystalReportViewermemberbandeghiri17 Mar '09 - 6:08 
QuestionCrystalReportViewer Email Button Help !memberkwankre20 Dec '08 - 1:44 
AnswerRe: CrystalReportViewer Email Button Help !membersam_333323 Aug '09 - 12:41 
GeneralRe: CrystalReportViewer Email Button Help !memberAMIT KUMAR JAISWAL26 Aug '09 - 21:56 
Generalcrystal report error "Load report failed"memberda_dhanaraj21 Oct '08 - 6:33 
GeneralRegarding dataset in C#membersaurabh_s19 May '08 - 21:17 
GeneralRe: Regarding dataset in C#memberAnbazhagan9 Oct '08 - 23:42 
QuestionQuestion?memberali_reza_zareian20 Jan '08 - 5:31 
QuestionUsing Collections instead of DataSetmemberJawad Munir26 Feb '07 - 20:27 
GeneralmemberMember #223058626 Feb '07 - 20:23 
Generalfrom multiple tablesmemberdeewar7 Feb '07 - 2:37 
General.resx file missing from download [modified]memberDr Reedo29 Jun '06 - 22:41 
Generalif i work with related tablesmemberdede1123 Mar '06 - 19:56 
GeneralPlease correct all URLs for images and code downloadmembergaryyuan16 Jan '06 - 12:45 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Jan 2006
Article Copyright 2006 by Ishara Gunarathna
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid