Click here to Skip to main content
Click here to Skip to main content
Go to top

Crystal Reports 2008 Dynamic Connection to Embedded Reports

, 13 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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# 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)

Share

About the Author

Mik Wadstrom
Software Developer (Senior)
Thailand Thailand
I Have worked as a programmer/system developer/system architect since 1986.
Worked as a contractor since 1993 for large to medium size companies like, Volvo, Gambro, EC Europe, IKEA, Ericsson mobile platform (EMP), Tecator, EireCom, Scandinavian Airline System IT and many more.
Now working and living in Thailand.
The reason for my move here 7 years ago was to lower my workload, geez how naive I was.
It took me 4 years of programming to make my own POS system, targeted towards computer illiterate people.
My POS and hotel management systems are made for people without any prior knowledge of computers and it has been and is a real challenge to make software so easy for any employee to use without weeks of learning.
It takes me roughly 4 hours to teach a person without any prior knowledge of computers to use my systems.
My aim is and has always been, make software that people can use without being a computer wiz.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 13 Feb 2014
Article Copyright 2014 by Mik Wadstrom
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid