Crystal Reports in C#

This is the C# Crystal Reports step-by-step article series. In this series we are learning various concepts and uses of Crystal Reports.

This article is for beginners creating their Crystal Reports for the first time in C#. Here we use a StudentDB database with the following tables. First of all create a database and two tables as in the following ways.

USE [StudentDB]

GO

/****** Object:  Table [dbo].[Teacher]    Script Date: 02/06/2014 22:18:10 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[Teacher](

            [TeacherId] [uniqueidentifier] NOT NULL,

            [Tname] [varchar](max) NULL,

            [Taddress] [varchar](max) NULL,

            [Subject] [nvarchar](max) NULL,

            [Standard] [nvarchar](max) NULL

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO

/****** Object:  Table [dbo].[Student]    Script Date: 02/06/2014 22:18:10 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[Student](

            [StudentId] [uniqueidentifier] NOT NULL,

            [Sname] [varchar](max) NULL,

            [Saddress] [varchar](max) NULL,

            [Standard] [varchar](max) NULL,

            [Division] [nvarchar](max) NULL

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO

We use the above database and table in this article series. Now let's our practical implementation of Crystal Reports step-by-step.

1. Open Visual Studio and create a new Windows Forms application.

Now you will get the default Form1.cs.

2. Now add Crystal Reports report using "Add" -> "New Item". Then the Add New Item dialogue will appear and select Crystal Reports from the box.

3. Select a report type from the Crystal Reports gallery.
 
Accept the default setting and click "OK".

4. The next step is to select an appropriate database connection. Here we used an OLEDB Connection for SQL Server to connect to Crystal Reports in C#.

The next step is to select the database provider.

Here we select Microsoft OLEDB provider for SQL Server.

The next screen is the SQL Server authentication for connecting to the database. For that we provide the server name, user id, password and select your Database Name and so on.
 
Click next, then leave that form as it is and click the Finish button.

After you click the Finish button, the next window is as follows.
 
Then click the StudentDB database. It will display a list of tables then double-click the Student table then you can see that the table will come in the right side list, as follows.
 
Click the "Next" button. Select all fields of the table to the right side list, it will look like this.
 
Click the "Finish" button. You will see the Crystal Reports designer window in your C# project. In this window you can see all fields you selected from the table in designer view. You can arrange the field objects and design screen depending on your requirements. It will look like this.
 
5. Now the design part is over now; it's time to view the Crystal Reports report in your C# Windows application form.
 
6. The next step is to drag and drop a new Crystal Reports Viewer from the tool box. After you drag the Crystal Reports viewer it looks like this.
 
7. Now you need to include the following CrystalDecisions.CrystalReports.Engine in your project as in the following:
 
using CrystalDecisions.CrystalReports.Engine;

8. The next step is to write some code for the button; double-click that to show a report button and paste the following code in that.

private void btnShow_Click(object sender, EventArgs e)

{

       ReportDocument cryRpt = new ReportDocument();

       cryRpt.Load(@"D:\C# Demos\Crystal Reports\CrystalReportDemo\CrystalReportDemo\CrystalReport1.rpt");

       crystalReportViewer1.ReportSource = cryRpt;

       crystalReportViewer1.Refresh();

}

9. Now run your project and click the show report button, it will look like this:

Report Button
 
When you click show report it will will ask user name and password of a database. Enter your user name and password and the result will look like this:

show report

Conclusion

In this article we learned how to use Crystal Reports and add a Crystal Reports viewer in a Windows Forms application and how to connect with the database. I hope this example will help you to learn the basics of a C# Crystal Reports application.

Up Next
    Ebook Download
    View all
    Learn
    View all