Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Problem Pringing Reports in C#

I have developed an app in c#, in visual studio.net 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

                       Form.Application_Form_Print();
                       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?

Thanks


 
0
polynominal
Asked:
polynominal
1 Solution
 
topdog770Commented:
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;
                  }

                  try
                  {
                        // 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;
                              crTable.ApplyLogOnInfo(crTableLogOnInfo);                         

                              //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)  ;
                        }
                        rpt.Refresh();
                  }
                  catch( Exception e )
                  {
                        // TODO: wire this into an application error handler
                        System.Windows.Forms.MessageBox.Show( e.Message );
                        return false;
                  }
                  return true;
            }
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now