How to create Crystal report in C#: Steps

How to create Crystal report:

1) From the main menu in Visual Studio C# project select PROJECT-->Add New Item .
 Then Add New Item dialogue will appear or (from Reports) and select Crystal Reports from the dialogue box. Give name for report

2)  Select Report type from Crystal Reports gallery.

3) Next step is to select the appropriate connection to your database .
 Here we are going to select OLEDB Connection for SQL Server to connect Crystal Reports in C#.
 Select OLE DB (ADO) from Create New Connection.

4) Select Microsoft OLE DB Provider for SQL Server .

5) The next screen is the SQL Server authentication screen for connecting to the database -
 Select your Sql Server name , enter userid , password and select your Database Name.

6) Click next , Then the screen shows OLE DB Property values , leave it as it is , and then click finish button.

7) After you click the finish button , the next window you will get your Server name under OLEDB Connection, from there selected database name  and click the tables, then you can see all your tables from your database.

8) Select all fields from  table to the right side list.

9) Click Finish Button. Then you can see the Crystal Reports designer window in your C# project.
 In the Crystal Reports designer window you can see the selected fields from Product table.
 You can arrange the field Objects and design of the screen according your requirements.

10) Now the designing part is over and the next step is to call the Crystal Reports in your C# application
and view it through Crystal Reports Viewer control in C#. Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

11) You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code."using CrystalDecisions.CrystalReports.Engine;"

In this case everytime it will ask user name and password.

In that case we need to write code in forms
using CrystalDecisions.Shared;

ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables ;

            cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");

            crConnectionInfo.ServerName = "YOUR SERVER NAME";
            crConnectionInfo.DatabaseName = "YOUR DATABASE NAME";
            crConnectionInfo.UserID = "YOUR DATABASE USERNAME";
            crConnectionInfo.Password = "YOUR DATABASE PASSWORD";

            CrTables = cryRpt.Database.Tables ;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();

Or

 ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(@"C:\Users\job\Documents\Visual Studio 2008\Projects\Stock_Test\Stock_Test\CrystalReport1.rpt");
            cryRpt.SetDatabaseLogon("User Name","Password","Server Name", "Database");
            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();

No comments:

Post a Comment