Thursday, April 19, 2012

How to Create a Basic Crystal Report using Windows Forms and C#

From this post, I will show you how to create a basic Crystal Report. You don't need to have previous knowledge in reporting. Just a little bit knowledge in C# and SQL would be enough.

In SQL Server, I'm creating a database named 'Company' and inside that I have a table named 'Employee'. An employee has an id, a name and a designation. Sample values are added into the Employee table.

Then in Microsoft Visual Studio, I'm creating a Windows Forms Application.

Then I need to add a data source to the project. Right click on the project in Solution Explorer. Select Add and Click on New Item.
Then in the dialog box, select 'Data' as the Category and from Template, select DataSet. The name for the new data set is given as EmployeeDataSet.xsd in this example. Then click Add.

Then the EmployeeDataSet.xsd file will be opened. If not open it from the Solution Explorer. In the Toolbox, under DataSet, click on the TableAdapter, drag it to the working area and drop it.
Then the Table Adapter Configuration Wizard will be opened.
Click on New Connection. Then give the database details appropriately. In this example, server name is localhost and the database is Company. Then test the connection.
Once the connection is successful, click OK.
Now the connection string is created. Click next in the Table Adapter Configuration Wizard.
In the next window, you can give a name for the connection string. For my convenience, I'll leave the default name. Then click Next.

In the next window, select 'Use SQL Statements' and click Next.
In the next window, you can specify the SQL query for the table adapter. I'll use the Query Builder to build the query.
In the Add Table window, select the tables you want for the query and click on Add. Then close the window. In this example I'm adding the Employee table.
\
Then select the columns needed for the query and click Execute Query to view the result. Then click OK.

Now the query is successfully built. Click Next.

Make the selections as shown in the following image and click Next.
 Now the table adapter is successfully configured. Click Finish.

Now you can see the configured table adapter in the working area.

Now we need to add a Crystal Report to the project. Right click on the project in the Solution Explorer and  select Add -> New Item. From the categories, select Reporting and from the Templates, select Crystal Report. The name given for the report in this example is EmployeeReport.rpt . Then click Add.
 Here I'm creating a Standard Report using the Reporting Wizard.
In the wizard, select the data table that you want in the report. Here I'm selecting the Employee table. Then click Next.
Then select the fields to be displayed in the report and click Next.

Next thing is Grouping. Here I don't want to group the displaying fields. So I'll click Next without making any selection.
Next thing is filtering fields. Here I don't need any filtering so I'll click Next.
Next thing is selecting a style for the report. I'll select Standard style and click Finish.
Now the report is created. You can drag the fields and customize the appearance. Add lines and images if you need. I'll just leave it as it is.
Now the designing is over. Now we need to view the report. Get the Windows Form (here Form1). In Tool box, under Reporting menu, click on the Crystal Report Viewer and drag it to the form and drop. 


Now we need to write little bit coding to display data on the report viewer. Go to the Form Load event and write the following code.

Now run the project and see the output report.




Feel free to put your comments here !

-Tharindu Edirisinghe
-SLIIT 10'

66 comments:

Anonymous said...

Thank you very much.!! keep it up good work always... :)

Tharindu Edirisinghe said...

Anytime :)

Jaypalsinh said...

how to create dynamic crystal report in windows application . and my database is password protected so i want to fetch records using where query on load. please help me.

Tharindu Edirisinghe said...

this is the normal way
http://thariya.blogspot.com/2012/04/how-to-create-basic-crystal-report.html

If the database is password protected, you can configure that in an App.config file

චරණ said...

Thank You Very Very Much Dear .. !!
Keep It Up Dude......

චරණ said...

Thank You Very Very Much Dear ...!!!
Keep It Up Dude ......

Anonymous said...

Thank you So much...!!!

Anonymous said...

hey bro.. can u give some tips for SEII

Tharindu Edirisinghe said...

drop me a mail via thariyarox@gmail.com . I'll give u some tips on SE II.

Anonymous said...

I would like to prepare a crystal report without using wizards after getting data source and connection string and would like to display it in a web form.

Thanks very much

Srinath Weerasinghe said...

Thanx very much Bro.

Elvin Mammadov said...

Hi,, I can not add the CrystalReportViewer in my repoert tab,, please, helm me

Tharindu Edirisinghe said...

What is the visual studio version you are using ? If it is 2010, Crystal Reports do not come inbuilt. You have to manually install it. Comment here your problem and I'll help you with that...

abad said...

i already have db i just want to show database on report page plz help me how can i do that

Tharindu Edirisinghe said...

It's the same way as mentioned in the post. In the beginning of the post I create the database and tables. Since you already have the database, start from the middle of the post and follow the steps.

Anonymous said...

Will You Please guide me how to generate reports from multiple tables?

Tharindu Edirisinghe said...

Same as above, in the Query Builder, add the tables you want and select the fields you want to be included in the report.... and done... :)

pakcikpopo said...

u give me an idea!!!thank u very much!!i newbie to crystal report and vb.

Lasitha Ranasinghe said...

Thanx a lot machan

Anonymous said...

hi how to create reports using crystal reports in visual studio 2010, crystal report is downloaded and installed, i want to know how to generate reports in windows aplication

Tharindu Edirisinghe said...

It's pretty much the same.. Didn't you try the above steps in VS 2010 ??

Anonymous said...

new SampleCrystalReport. ??? which is that??

yoggy marshmallows. said...

Hi. How will you generate a report in Crystal report with a string parameter in c#.Hope you can help me.. thanks

Tharindu Edirisinghe said...

Hi yoggy marshmallows, Check the following link
http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-string-parameter.htm

yoggy marshmallows. said...

I already checked it and try but there's an error there in the line "cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");"
I put the fullpath of the crystal report but theres an error. on that. hmm any idea on how to do that? Thanks

For Music Lovers said...

really usefull

Brijesh Kumar M Prajapati said...

Thanks dude Work Sucessfull Always

--> Can u tell me how to Set a height & width report
--> Exmple 1)Report Envelop Size 2) Cheque Size Both Are Work Single Project

Rashardh Muhammadh said...

Really Useful Brothers.. Keep Going

ManiKandan.S said...

Hi,
I have a image path in table. How do i display the image in crystal report by using image path.

Tharindu Edirisinghe said...

Search in Youtube. You can find the answer.

kalpana said...

i want to create a crystal report in c#.net windows application ...............
plz any one can help me how to display report name wise o

kalpana said...

i want to create a crystal report in c#.net windows application ...............
plz any one can help me how to display report name wise o

mzonerz said...

nice work

mzonerz said...

thanx

pankaj said...

v v nice

Sohan Mahto said...

I want to write in words of sum to TotalAmount in report. Help me....

Tharindu Edirisinghe said...

@Sohan - if you can provide more information, I'll be able to help you.

Masoom banfm said...

thanks a lot
if i want to build a query with a Where clause what i should do?
really I have a form Like a bill and I want to send just the new bill
first it save to db and then send to crystal report
how I do this ??!!

Tharindu Edirisinghe said...

@Masoom -

It's not much different to this... just google for "parameterized crystal reports" and you can find plenty of examples.

Madanga Bandara said...

Good work aiya.........

Madanga(SLIIT Student)

Madanga Bandara said...

Good work aiya keep it up............!

Madanga Bandara said...

Good work aiya.........

Madanga(SLIIT Student)

Althaf Ahamed said...

i cant understood emplyeedatatableadapter.empyoyeeTableAdapter ta

the first word of 4th line

Tharindu Edirisinghe said...

@Althaf - it starts with the name Employee.... because the table name here is Employee. If you have a table name with a different name, the table adapter class name will begin with that name.

Mohammad Taqi Ratnani said...

sir by running your code i found 21 error one of which is
Error 31 The name 'CachedReportConstants' does not exist in the current context

Tharindu Edirisinghe said...

@Mohammad - Check if the following link is useful
http://social.msdn.microsoft.com/Forums/en-US/8c1c74c6-3916-460a-9fef-f211df75898e/crystal-reports-not-working-on-preexisting-project

lingmaaki said...

A full tutorial http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm c# crystal reports tutorial

maaki

Patel Hardik said...

when i create crystal report in vs 2010 using sql data base i face error

Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.

then i search on google n find solution to add below code in app.config






but my application give me some error like


Could not find schema information for the element 'log4net'.
Could not find schema information for the element 'appendToFile'.
Could not find schema information for the attribute 'value'.
Could not find schema information for the element 'rollingStyle'.
Could not find schema information for the attribute 'value'.
Could not find schema information for the element 'maxSizeRollBackups'.
Could not find schema information for the attribute 'value'.
Could not find schema information for the element 'maximumFileSize'.
Could not find schema information for the attribute 'value'
.Could not find schema information for the element 'staticLogFileName'.



Any one help me pls ..

Ashok said...

Very nice dude.. :) keep it up..

Bima Putra said...

Can't detect the database

Tharindu Edirisinghe said...

Hi Bima, check whether you have entered the Server name correctly

බුද්ධික said...

I'm using VB.NET 2012 express and I couldn't find reporting on Add item in project,is there any way to create a report on VB.NET 2012 express

Ratan kushwaha said...

I amusing C#.NET 2012 express and I couldn't find reporting on Add item in project,is there any way to create a report on C#.NET 2012 express

Ratan kushwaha said...

thanx .....

Arunkumar Murugesan said...

Hi Tharindu Edirisinghe,
I generate the crystal reports as it is your articles. It works till the end, but when i run the coding it produce the empty screen with date only. I couldn't able to find where i made mistakes. can you please help me.
thank you,
Arunkumar M

Tharindu Edirisinghe said...

Hi Arunkumar, I'd like to help. Drop me an email through the contact me page in the top menu and I'll contact you through emails.

Vibhatha Abeykoon said...

Awesome Carry On the Good Work.. :D (y)

Neo Keanu said...

thanx, finally your blog helped me ,, ! great work sir

sandip kushvah said...

Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.
why this error shows???help me

sandip kushvah said...

Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.

why this error occur??
help me to solve this error.

Bonaventure Ikenna said...

Thank you very Much Boss.. Your simple material has changed my entire programming career for good... How I wish you will grant me access to more of your C# Vs SQL Server 2012 materials. I will be very happy my able Mentor.. [my mail : ngwubonaventureikennadj@gmail.com] Thanks Boss

Bonaventure Ikenna said...

Thank you very Much Boss.. Your simple material has changed my entire programming career for good... How I wish you will grant me access to more of your C# Vs SQL Server 2012 materials. I will be very happy my able Mentor.. [my mail : ngwubonaventureikennadj@gmail.com] Thanks Boss

lingmaaki said...

More on crystal reports....

Crystal Reports Tutorial

Ling

Nor diana said...

Can I know either empty website able to create crystal report or not? its because I have already done my project and I just realized that to create crystal report we need using windows form application. Can you have help me to solve this problem?

raja said...

quite ok

r edison said...

hai,

Its realy helpfull to me.....thank you brother.

Post a Comment