5,276,406 members and growing! (16,826 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Creating Crystal Reports using C# with Datasets

By Ishara Gunarathna

This is a sample that guide you to create reports with Crystal Reports using C# Datasets, which is the best and the easiest way to create reports
C#, Windows, .NET, Visual Studio, Dev

Posted: 13 Jan 2006
Updated: 13 Jan 2006
Views: 79,411
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 4.15 Rating: 3.05 out of 5
4 votes, 17.4%
1
1 vote, 4.3%
2
3 votes, 13.0%
3
6 votes, 26.1%
4
9 votes, 39.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

 

Sample screenshot

 

Introduction

This article is about creating Crystal Reports using C#.NET. About two weeks back, one of my friends asked me this kind of example, also he searched through the Internet and he told that he couldn’t find a sample. When I was doing this kind of developments I’m also looked for samples, but I couldn’t find one. That’s why I did this article to CodeProject without creating reports separately using Crystal Reports other than using .NET IDE.

Create a Dataset

First you should create a Dataset to get the 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 is looks like the following window and can add elements to the Dataset by dragging and dropping elements from the toolbox. This Dataset is used to fill the data, which is getting 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 you have a created Datset, then we can use it to fill the report with data, which will be getting from the DB. As I said 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

Next window is 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

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

Sample screenshot

Then your report creation is done by the .NET IDE. If you want to do any changes to the report you can do it 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. And also you need to set the report source of this “crystalReportViewer” component, which is falling 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 and fill it with data getting

// 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

 

Also you can 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.

Ex:-

 

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

 

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

 

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

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ishara Gunarathna


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

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralRegarding dataset in C#membersaurabh_s22:17 19 May '08  
QuestionQuestion?memberali_reza_zareian6:31 20 Jan '08  
QuestionUsing Collections instead of DataSetmemberJawad Munir21:27 26 Feb '07  
Generalmember21:23 26 Feb '07  
Generalfrom multiple tablesmemberdeewar3:37 7 Feb '07  
General.resx file missing from download [modified]memberDr Reedo23:41 29 Jun '06  
Generalif i work with related tablesmemberdede1120:56 23 Mar '06  
GeneralPlease correct all URLs for images and code downloadmembergaryyuan13:45 16 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jan 2006
Editor:
Copyright 2006 by Ishara Gunarathna
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project