Problem Pringing Reports in C#

Posted on 2006-04-26
Last Modified: 2010-04-16
I have developed an app in c#, in visual that prints out word documents from a folder on a network. This works fine.

I have now added a crystal report to it, developed in CR10.

I have added it using Project > Add Existing Item.

I then used the toolbox to add the component ReportDocument to the form.

I want to print the report out after the word documents without previewing the report in a viewer.

The code below has been generated for the report, I have used the line application_Form___Overnight_Print1.PrintToPrinter(1,true,1,1); to try and print it

                       this.Application_Form_Print1.FileName = "";
                      this.Application_Form_Print1.InitReport += new (System.EventHandlerthis.Application_Form_Print1_InitReport);
                  // Form1

                 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                 this.ClientSize = new System.Drawing.Size(600, 470);
                 this.Name = "Form1";

private void application_Form___Overnight_Print1_InitReport(object sender, System.EventArgs e)

            {            application_Form___Overnight_Print1.PrintToPrinter(1,true,1,1);


I run the report, the word documents print as normal, but no reports are printed to the printer, and I do not receive any error messages.

I have developed the report off a SQL database in CR10 using an odbc connection. The database has a userid and password.

Am I getting nothing because I have not connected to the database and how would I do this or is it something else?

I would also like to specify the printer name to print the reports from and wandered how to do this, the printers are networked and are in the format WSI-01/prn001

Can anybody help with this?


Question by:polynominal
    1 Comment
    LVL 12

    Accepted Solution

    Here's some code snippets that may help.

    It's also listed in the order you have to initialize things for crystal reports.
    This code is for a report that directly connects to a database, and takes a parameter

    ReportDocument crReport;
    ReportDocument _crReport;
    private Database crDatabase;
    private Tables crTables;
    private Table crTable;
    private TableLogOnInfo crTableLogOnInfo = null ; //new TableLogOnInfo();
    private ConnectionInfo crConnectionInfo = null; //new ConnectionInfo ();

    this._crReport= new ReportDocument();
    crTableLogOnInfo = new TableLogOnInfo();
    crConnectionInfo = new ConnectionInfo ();

    // for oracle, database name not needed
    crConnectionInfo.ServerName = AppConstants.crServerName;
    crConnectionInfo.DatabaseName = "";
    crConnectionInfo.UserID = AppConstants.crUserName;
    crConnectionInfo.Password = AppConstants.crUserPassword;

    this.__crReport.Load( AppConstants.crReportPath );

    SetReportLogon( ref ReportDocument rpt )

    _crReport.SetParameterValue(name, value);

    _crReport.PrintOptions.PrinterName = UserSettings.GetInstance().LocalReportPrinterName;

    _crReport.PrintToPrinter(Copies, false, 1, 1);

    private bool SetReportLogon( ref ReportDocument rpt )
                      if( !rpt.IsLoaded )
                            // error handling should be triggered here
                            System.Windows.Forms.MessageBox.Show("Unable to load report");
                            return false;

                            // This code works for both user tables and stored procedures
                            //Get the table information from the report
                            crDatabase = rpt.Database;
                            crTables = crDatabase.Tables;

                            //Loop through all tables in the report and apply the
                            //connection information for each table.
                            for (int i = 0; i < crTables.Count; i++)
                                  crTable = crTables [i];
                                  crTableLogOnInfo = crTable.LogOnInfo;
                                  crTableLogOnInfo.ConnectionInfo = crConnectionInfo;

                                  //If your DatabaseName is changing at runtime, specify
                                  //the table location. For example, when you are reporting
                                  //off of a Northwind database on SQL server
                                  //you should have the following line of code:
                                  //crTable.Location = AppConstants.crServerName  + "."  + crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)  ;
                      catch( Exception e )
                            // TODO: wire this into an application error handler
                            System.Windows.Forms.MessageBox.Show( e.Message );
                            return false;
                      return true;

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    How to parse date from file path? 4 31
    Global Keyboard Hooks Blocked 4 37
    Explain Unit of Work pattern 2 27
    Linq help 1 0
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now