Very new to C#
here is the code
this works fine but if I run more than 3 reports I get the error
I have had to put a lock which stops it from breaking but I would like to resolve it properly.
any help would be really appreciated
-Mo
here is the code
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Threading;
namespace Acumen.Reports
{
public class CrystalReportGenerator
{
private ParameterDictionary _parms;
public ParameterDictionary parms
{
get { return _parms; }
set { _parms = value; }
}
public CrystalReportGenerator(string parameterString)
{
_parms = new ParameterDictionary(parameterString);
}
public CrystalReportGenerator(ParameterDictionary parms)
{
_parms = parms;
}
public void Process()
{
LogonDescriptor logonDetails = new LogonDescriptor(parms);
string reportName = String.Concat(parms["ReportPath"], parms["ReportName"]);
string exportPath = String.Concat(parms["ExportPath"], parms["ExportName"]);
string time = Regex.Replace(Regex.Replace(Regex.Replace(System.DateTime.Now.ToString(), ":", ""), "/", ""), " ", "");
exportPath = Regex.Replace(exportPath,".pdf",time + ".pdf");
Object thisLock = new Object();
lock(thisLock)
{
ReportDocument reportDocument = new ReportDocument();
try
{
reportDocument.Load(reportName);
//set the logon parameters for the report
reportDocument.SetDatabaseLogon(logonDetails.UserName, logonDetails.UserPassword, logonDetails.ServerName, logonDetails.DatabaseName);
//set the logon parameters for possible subreports
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
subReportDocument.SetDatabaseLogon(logonDetails.UserName, logonDetails.UserPassword, logonDetails.ServerName, logonDetails.DatabaseName);
}
}
}
//add the loop for the parameters input
ParameterFields Fields = new ParameterFields();
for (int i = 0; i < reportDocument.DataDefinition.ParameterFields.Count; i++)
{
ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.ParameterFieldName = reportDocument.DataDefinition.ParameterFields[i].ParameterFieldName;
paramDiscreteValue.Value = parms[reportDocument.DataDefinition.ParameterFields[i].ParameterFieldName];
paramField.CurrentValues.Add(paramDiscreteValue);
reportDocument.SetParameterValue(paramField.ParameterFieldName, paramDiscreteValue.Value);
}
//set the destination parameter for the export option
DiskFileDestinationOptions fileOptions = new DiskFileDestinationOptions();
fileOptions.DiskFileName = exportPath;
reportDocument.ExportOptions.DestinationOptions = fileOptions;
reportDocument.ExportOptions.UExportDestinationType = (int)ExportDestinationType.DiskFile;
reportDocument.ExportOptions.UExportFormatType = (int)ExportFormatType.PortableDocFormat;
reportDocument.Export();
}
catch
{
Console.WriteLine("What the hek's going on?");
}
finally
{
reportDocument.Close();
}
reportDocument.Dispose();
}
}
}
}
this works fine but if I run more than 3 reports I get the error
Code:
A first chance exception of type 'System.IO.IOException' occurred in CrystalDecisions.CrystalReports.Engine.dll
I have had to put a lock which stops it from breaking but I would like to resolve it properly.
any help would be really appreciated
-Mo