Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Crystal Reports 2008 Dynamic Connection to Embedded Reports

Rate me:
Please Sign up or sign in to vote.
4.82/5 (4 votes)
13 Feb 2014CPOL 10.3K   4  
Simple way to use Crystal reports as embedded dynamic printable reports

Introduction

This tip shows how to use Crystal Reports to print without using the print preview control with any report ".rpt".

Background

Unfortunately over the years, Crystal Reports have become a monster to both understand and use.

I have used this code for a few years now and it works with any type of report created with CR12 to print embedded reports in projects.

This sub could be put in any class as long it will expose itself to the project.

Make sure the report is included and embedded in your project.

C#
//C# example for the LogOnInfo routine.

internal void setConSQLRPT(CrystalReports.Engine.ReportDocument rptReport, string strReportName)
{
  string strServer = My.Computer.Name; //Any database, even network DB
  string DBInstance = "SQLExpress";  //Any instance 
  ConnectionInfo connection = new ConnectionInfo();
  connection.DatabaseName = "MYDB";
  connection.AllowCustomConnection = true;
  connection.ServerName = strServer + @"\" + DBInstance;
  connection.IntegratedSecurity = false;
  connection.Type = ConnectionInfoType.SQL;
  connection.UserID = "USERNAMEINDB";
  connection.Password = "XXXXX";


  TableLogOnInfo logOnInfo = new TableLogOnInfo();
  logOnInfo.ReportName = strReportName;

  foreach (CrystalDecisions.CrystalReports.Engine.Table myTable in rptReport.Database.Tables) {
    logOnInfo = myTable.LogOnInfo;
    logOnInfo.ConnectionInfo = connection;
    myTable.ApplyLogOnInfo(logOnInfo);
    myTable.TestConnectivity();
  }
}
//

//Use like below
//References used
using CrystalDecisions;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
//
CrystalDecisions.CrystalReports.Engine.ReportDocument mwPRT;
                    
mwPRT = new EmbeddedReportName();

setConSQLRPT(mwPRT, "EmbeddedReportName.rpt");
mwPRT.Refresh();

//Add and set parametervalues if you have any in your report

mwPRT.SetParameterValue("CompanyName", Value1);
mwPRT.SetParameterValue("Fontalign", Value2);
mwPRT.SetParameterValue("Font", Value3);
mwPRT.SetParameterValue("FontSize", (float)Value4);

//Code how to find printers either in your network or local computer, you can find in CodeProject 
//Choose which printer to use
System.Drawing.Printing.PrinterSettings oPrinterSettings = new System.Drawing.Printing.PrinterSettings();
oPrinterSettings.PrinterName = "Brother MFC-6490CW";
mwPRT.PrintOptions.PrinterName = oPrinterSettings.PrinterName;

//and Print. To change papersize is still a problem, default is what size at the time of creation of the report.
//parameters are,(number of copies, Collation , StartPage, EndPage)   
mwPRT.PrintToPrinter(1, false, 1, 99);
//and close instance of CR report 
mwPRT.Close();

Voila, a printed report.

You can even use SQL queries from your own code and pass in the result to the embedded report instead of relying on CR to create the result, could be much faster to print if the query is already done within your method, depending on the complexity of the query.

If anyone is interested to know how, I will edit later.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --