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'

73 comments:

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

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

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

    ReplyDelete
  6. Thank you So much...!!!

    ReplyDelete
  7. hey bro.. can u give some tips for SEII

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

    ReplyDelete
  9. 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

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

    ReplyDelete
  11. 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...

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

    ReplyDelete
  13. 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.

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

    ReplyDelete
  15. 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... :)

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

    ReplyDelete
  17. 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

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

    ReplyDelete
  19. new SampleCrystalReport. ??? which is that??

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

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

    ReplyDelete
  22. 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

    ReplyDelete
  23. 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

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

    ReplyDelete
  25. Search in Youtube. You can find the answer.

    ReplyDelete
  26. 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

    ReplyDelete
  27. 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

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

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

    ReplyDelete
  30. 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 ??!!

    ReplyDelete
  31. @Masoom -

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

    ReplyDelete
  32. Good work aiya.........

    Madanga(SLIIT Student)

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

    ReplyDelete
  34. Good work aiya.........

    Madanga(SLIIT Student)

    ReplyDelete
  35. i cant understood emplyeedatatableadapter.empyoyeeTableAdapter ta

    the first word of 4th line

    ReplyDelete
  36. @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.

    ReplyDelete
  37. 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

    ReplyDelete
  38. @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

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

    maaki

    ReplyDelete
  40. 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 ..

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

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

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. 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

    ReplyDelete
  45. 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

    ReplyDelete
  46. 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.

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

    ReplyDelete
  48. thanx, finally your blog helped me ,, ! great work sir

    ReplyDelete
  49. 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

    ReplyDelete
  50. 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.

    ReplyDelete
  51. 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

    ReplyDelete
  52. 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

    ReplyDelete
  53. 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?

    ReplyDelete
  54. hai,

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

    ReplyDelete
  55. I developed a report in crystal report in C# Windows application but unable to view the full report in screen. For view the report I am scrolling the mouse up & down pls help.

    ReplyDelete
  56. Hi,@Tharindu Edirisinghe,
    I want to create a crystal report on selection of some radio button,combo box, and after that on click of generate button report form is shown, I want to show that field of data which i had selected by radio button, combo box. My database is in Access. That means i need a particular field of data not all.
    PLZ suggest me some link or plz do some for me.

    ReplyDelete
  57. Hello all members plz tell me how to search in crystal report using textbox windows application using c#

    ReplyDelete
  58. Hi,

    I am using Visual Studio Ultimate 2013 and I can't see Crystal Reports Template. And I am using MS Access Database. How do I go about this? Please help. Thanks.

    ReplyDelete
  59. Dear Friend,
    Very useful post
    I have been searching this type of guide for a long time.
    Thanks a lot ....

    ReplyDelete