Crystal Reports - sorry to bring the pain

M

Matt M

Hey... I'm trying to run a crystal report (8.5) from a c# windows forms
application class. I can't use the report viewer as the application will be
running these reports from a command line interface (for daily report
generation). I have looked high and low for some samples and/or some help
on what references to use, how to even create a report object etc, but to no
avail. Can anybody please help me (links to references would be just
dandy). I know, there's probably a newsgroup for this in particular, but I
couldn't find it.

Thanks.
 
M

Matt M

I've solved this on my own. Here's the code (not adjusted to be readable -
you should be able to figure it out):

Make sure you're referencing and "using"

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;


ReportDocument crReportDocument = new ReportDocument();

ExportOptions crExportOptions;

DiskFileDestinationOptions crDiskFileDestinationOptions;

ConnectionInfo crConnectionInfo;







foreach (XmlNode xn in xl)

{

if (xn.Attributes["Name"].Value.ToLower().Trim() ==
sReportName.ToLower().Trim())

{

//run the report

//set connection settings (stupid Crystal)

crConnectionInfo = new ConnectionInfo();

crConnectionInfo.ServerName =
xn.SelectSingleNode("ConnectionInfo").Attributes["Server"].Value;

crConnectionInfo.DatabaseName =
xn.SelectSingleNode("ConnectionInfo").Attributes["Database"].Value;

crConnectionInfo.UserID =
xn.SelectSingleNode("ConnectionInfo").Attributes["User"].Value;

crConnectionInfo.Password =
xn.SelectSingleNode("ConnectionInfo").Attributes["Password"].Value;

//load the report

crReportDocument.Load(xn.SelectSingleNode("Location").InnerText);



//stupid Crystal Reports requirement - set the login information for every
table.

Tables crTables = crReportDocument.Database.Tables;

foreach (Table crTable in crTables)

{

TableLogOnInfo crTableLogonInfo = crTable.LogOnInfo;

crTableLogonInfo.ConnectionInfo = crConnectionInfo;

crTable.ApplyLogOnInfo(crTableLogonInfo);

}


//Set the export destination

crDiskFileDestinationOptions = new DiskFileDestinationOptions();

string sExportFileName = Application.StartupPath + "\\" +
xn.Attributes["Name"].Value.Trim() + " " + DateTime.Now.ToString("dd") + "-"
+ DateTime.Now.Month + "-" + DateTime.Now.Year + " at " + DateTime.Now.Hour
+ "." + DateTime.Now.Minute + "." + DateTime.Now.Second + ".pdf";

crDiskFileDestinationOptions.DiskFileName = sExportFileName;

//'Set the exporting information

crExportOptions = crReportDocument.ExportOptions;

crExportOptions.DestinationOptions = crDiskFileDestinationOptions;

crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;

crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;


//Export the report

crReportDocument.Export();



}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top