locked
Print Crystal Report directly from C# without using Report Viewer RRS feed

  • Question

  • Hello All,

    I need to print a crystal report directly to the printer without using ReportViewer in C#. I've a CrystalReport2(belowCode) which requires a query (Select * from JobHeader where JobNumber='J012345') to display all the data. I'm stuck with database connection and putting all the data to DataAdapter in C# (Below Code). I also have 8 subreports in my CrystalReport2. Can somebody please give me hand on this? I'm struggling with it. Thanks.

    Regards

    using CrystalDecisions.CrystalReports.Engine;
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    namespace csharp_win_printtoprinter
    {
    	/// <summary>
    	/// Summary description for Form1
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    
    		//CR Variables
    		ReportDocument crReportDocument;
    
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.PrintDialog printDialog1;
    		private System.Windows.Forms.Button button2;
    		private System.Drawing.Printing.PrintDocument printDocument1;
            private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
            private CrystalReport1 CrystalReport11;
            private Chart Chart1;
    		/// <summary>
    		/// Required designer variable
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor
    		/// </summary>
    		private void InitializeComponent()
    		{
                this.button1 = new System.Windows.Forms.Button();
                this.printDialog1 = new System.Windows.Forms.PrintDialog();
                this.button2 = new System.Windows.Forms.Button();
                this.printDocument1 = new System.Drawing.Printing.PrintDocument();
                this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
                this.Chart1 = new csharp_win_printtoprinter.Chart();
                this.CrystalReport11 = new csharp_win_printtoprinter.CrystalReport1();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.button1.Location = new System.Drawing.Point(32, 56);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(160, 104);
                this.button1.TabIndex = 0;
                this.button1.Text = "Print Report No Choose Printer";
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // button2
                // 
                this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.button2.Location = new System.Drawing.Point(272, 56);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(160, 104);
                this.button2.TabIndex = 1;
                this.button2.Text = "Choose Printer";
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // crystalReportViewer1
                // 
                this.crystalReportViewer1.ActiveViewIndex = 0;
                this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
                this.crystalReportViewer1.Location = new System.Drawing.Point(47, 188);
                this.crystalReportViewer1.Name = "crystalReportViewer1";
                this.crystalReportViewer1.ReportSource = this.CrystalReport11;
                this.crystalReportViewer1.Size = new System.Drawing.Size(903, 296);
                this.crystalReportViewer1.TabIndex = 2;
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(1164, 505);
                this.Controls.Add(this.crystalReportViewer1);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			
                crReportDocument = new ReportDocument();
    			//Create an instance of a report
                crReportDocument = new CrystalReport2();
    
    			//Use error handling in case an error occurs
    			try
    			{
    				//Set the printer name to print the report to.  By default the sample
    				//report does not have a defult printer specified.  This will tell the
    				//engine to use the specified printer to print the report.  Print out 
    				//a test page (from Printer properties) to get the correct value.
    				
    
    				System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
    				crReportDocument.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;
    
    
    				//Start the printing process.  Provide details of the print job
    				//using the arguments.
    				crReportDocument.PrintToPrinter(0 ,true ,  1, 1);
    
    				//Let the user know that the print job is completed
    				MessageBox.Show("Report finished printing!");
    			}
    			catch(Exception err)
    			{
                    MessageBox.Show(err.ToString(), "Unexpected exception");
    			}
    		}
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    		
    		}
    
    		
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			//Open the PrintDialog
    			this.printDialog1.Document = this.printDocument1;
    			DialogResult dr =  this.printDialog1.ShowDialog();
    			if(dr == DialogResult.OK)
    			{
    				//Get the Copy times
    				int nCopy = this.printDocument1.PrinterSettings.Copies;
    				//Get the number of Start Page
    				int sPage = this.printDocument1.PrinterSettings.FromPage;
    				//Get the number of End Page
    				int ePage = this.printDocument1.PrinterSettings.ToPage;
    				string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
    
    				crReportDocument = new ReportDocument();
    				//Create an instance of a report
    				crReportDocument = new Chart();
    				try
    				{
    					//Set the printer name to print the report to.  By default the sample
    					//report does not have a defult printer specified.  This will tell the
    					//engine to use the specified printer to print the report.  Print out 
    					//a test page (from Printer properties) to get the correct value.
    				
    					crReportDocument.PrintOptions.PrinterName = PrinterName;
    
    
    					//Start the printing process.  Provide details of the print job
    					//using the arguments.
    					crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
    
    					//Let the user know that the print job is completed
    					MessageBox.Show("Report finished printing!");
    				}
    				catch(Exception err)
    				{
    					MessageBox.Show(err.ToString());
    				}
    			}
    
    			
    		}
    
    	}
    }
    


    • Edited by SSRS-Newbie Wednesday, October 8, 2014 10:09 PM
    • Moved by Carl Cai Thursday, October 9, 2014 3:14 AM more related to suggested forum
    Wednesday, October 8, 2014 10:03 PM

Answers

  • 1. This is a VB.Net forum not a Visual C# forum. For Visual C# questions try this forum.

    Visual C#

    2. Crystal Reports are not supported by Microsoft. Therefore, unless somebody responding in the Visual C# forum has experience with whichever version of Crystal Reports you are using, you will probably not receive an answer in the Visual C# forum. SAP has their own assistance for Crystal Reports using Visual Studio. You can try to find an appropriate forum to ask questions in at SAP in the below link.

    SAP Community Network


    La vida loca

    • Proposed as answer by Carl Cai Thursday, October 9, 2014 3:14 AM
    • Marked as answer by Just Karl Thursday, October 30, 2014 10:47 PM
    Thursday, October 9, 2014 1:59 AM