Pages

Showing posts with label crystal report. Show all posts
Showing posts with label crystal report. Show all posts

Thursday 18 June 2015

Code to EXPORT crystalreport to PDF

Introduction: In this article I will explain how to save the CrystalReport as PFD file. Saving pdf format of crystalreports is very important to keep the record.

Code :
Here is the complete code:

try
            {
                if (!Directory.Exists("D:\\Sanjeev\\"))
                {
                    Directory.CreateDirectory("D:\\Sanjeev\\");
                }
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = "D:\\Sanjeev\\newpdf.pdf";
                CrExportOptions = b.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }
                b.Export();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


Saturday 25 October 2014

Crystal Report in windows form application

Overview : Crystal reports are best used for printing document. Crystal reports provide a great printing experience. In this article I will tell you about how to add crystal report to your application and it's working.

Initially for this we need to take a DataSet from Add New Item in our application, and after adding the dataset we need to create a DataTable in our DataSet. I am creating a table with the name DataTable2. After adding table add some columns in your table as per your requirements. I am adding these columns as shown.
   

Now we need to add a crystalreport to our application from Add New Item in Reporting section as follows : 

After adding Crystal Report, a window named Database Expert appears, in this window expand Project Data, then expand ADO.NET Dataset then expand your dataset which you have created earlier and finally select your table and press OK.

Now you will see five sections in your crystal report as 
1. Section 1 (Report Header) : This section will be printed on the first page of the report.
2. Section 2 (Page Header) : This section will appear in all printed pages of your report.
3. Section 3 (Details) : This section will be repeated according to your data in DataTable.
4. Section 4 (Report Footer) : This section will be repeated once in a report.
5. Section 5(Page Footer) : This section will appear in footer of every page.

After placing data at appropriate place, your report is ready for print command, Now we will see the code to print the report.

The below shown code will be placed on the Click command of the button on which the report will be printed : 

First of all we will declare a DataTable with same name as in DataSet as follows : 

                            DataTable DataTable2 = new DataTable();
                            DataTable2.Columns.Add("date", typeof(string));
                            DataTable2.Columns.Add("agent", typeof(string));
                            DataTable2.Columns.Add("amount", typeof(string));
                            DataTable2.Columns.Add("receivedby", typeof(string));

now populate the DataTable with data as follows : 


                            DataRow dr = DataTable2.NewRow();
                            dr[0] = "column1";
                            dr[1] = "column2";
                            dr[2] = "column3";
                            dr[3] = "column4";

                            DataTable2.Rows.Add(dr);

After this the print command :

crystalreport1 crr1 = new crystalreport1();
crr1.Database.Tables["DataTable2"].SetDataSource((DataTable)DataTable2);
crystalReportViewer1.ReportSource = crr1;

This code will print the crystal report to your default printer.

NameSpace for this action : 

using System.IO;
using System.Drawing.Printing;


Happy Coding....