on 09-30-2015 7:10 PM
Hello Everyone,
I hit a wall while trying to output a crystal report in PDF format. After the report is loaded, I want to use the refresh method of the ReportDocument object so that the report can show the most update data then export it in PDF. But what I had been experiencing is that whenever I used the Refresh method the code always throws an exception in the line where the report needs to be exported in PDF format. When I don't use the Refresh method, the code doesn't break in the line where it has to export the document in PDF. I would like to know what alternative there could be. Because the code does what it was intended, which is connect to a database, run a stored procedure and then output the Crystal in PDF. It is just that the data shown is not up to date.
Any help would be truly appreciated.
Here the code:
ArrayList ParameterArrayList = new ArrayList(); //Report parameter list. Needs to be declared even if no parameters is being passed to the report
CrystalReportBase objReportBase = new CrystalReportBase(); //this is custom class that loads a crystal report, connect to a datasource, pass parameters to a report, and return a a return ReportDocument object
ReportDocument objReportDocument = new ReportDocument(); //Report document
try
{
string filterExtension = "*.rpt";
string substringInFileName = "M1_2_PhoneAnswerPerformance";
/*The report with two parameters. */
ParameterArrayList.Add(0);
ParameterArrayList.Add("No"); //Parameter 1 with input 1.
//ParameterArrayList.Add(1);
//ParameterArrayList.Add("1"); //Parameter 2 with input 1.
//retrieve connection information from the web2 connection string
objReportBase.ServerName = serverConnectionInfo.DataSource;
objReportBase.UserID = serverConnectionInfo.UserID;
objReportBase.Password = serverConnectionInfo.Password;
List<ReportFile> reportFiles = Utilities.GetFilesFromDirectory(@crystalPhoneReportPerformancePath, filterExtension, substringInFileName).ToList();
foreach (ReportFile reportFile in reportFiles)
{
objReportDocument = objReportBase.GenerateReport(reportFile.FileName, ParameterArrayList, crystalPhoneReportPerformancePath);
objReportDocument.Refresh(); Using this line causes....
//Save output report
objReportDocument.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, string.Format("{0}{1}{2}", @crystalPhoneReportOutputMemberServicesPath, Path.GetFileNameWithoutExtension(reportFile.FileName), ".pdf")); ...the code to break in this line
//Export this file in Excel format as well
if (reportFile.FileName.Contains("CLHS_M1_2_PhoneAnswerPerformanceGraphs_Combined"))
{
objReportDocument.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.ExcelWorkbook, string.Format("{0}{1}{2}", @crystalPhoneReportOutputMemberServicesPath, Path.GetFileNameWithoutExtension(reportFile.FileName), ".xls"));
}
}
//release those resources
objReportDocument.Dispose();
objReportDocument.Close();
objReportDocument = null;
catch{
1. Make sure that you do NOT have "Save Data with Report" turned on in the File menu. This is probably the cause of your old data.
2. It doesn't look like you're actually setting the parameters for the report in your code. If you go here http://scn.sap.com/docs/DOC-6948 and download the sample code, there is a sample available for how to set parameters which includes code that looks like this (I've tweaked it a bit to make it apply for more than one parameter):
private void SetCurrentValuesForParameterField(ReportDocument reportDocument, ArrayList arrayList, string paramName)
{
ParameterValues currentParameterValues = new ParameterValues();
foreach (object submittedValue in arrayList)
{
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
parameterDiscreteValue.Value = submittedValue.ToString();
currentParameterValues.Add(parameterDiscreteValue);
}
ParameterFieldDefinitions parameterFieldDefinitions = reportDocument.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[paramName];
parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);
}
You call this for each parameter. The ArrayList will contain a single value if this is a single-value parameter, it may contain multiple values if this is a multi-select parameter.
-Dell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Dell,
I unchecked the "Save Data with Report" option from the file menu. When I ran the report, it throws an exception at the line in Bold below. The message said, "Specified report path cannot be found". Which is NOT true. The physical path for the output file really does exists. I don't know where else to look. Would you be able to point me to some other directions? Thanks in advance for all your precious help.
ReportDocument objReportDocument = new ReportDocument();
string crystalPhoneReportPerformancePath = "C:\MyDocuments\CrystalReports\";
string filterExtension = "*.rpt";
string substringInFileName = "M1_2_PhoneAnswerPerformance";
List<ReportFile> reportFiles = Utilities.GetFilesFromDirectory(@crystalPhoneReportPerformancePath, filterExtension, substringInFileName).ToList();
foreach (ReportFile reportFile in reportFiles)
{
string reportFileNameWithoutExtension =Path.GetFileNameWithoutExtension(reportFile.FileName);
objReportDocument.Load(string.Format("{0}{1}",@crystalPhoneReportPerformancePath, reportFile.FileName));
objReportDocument.SetParameterValue("FallonSemiAnnual", "No");
objReportDocument.SetDatabaseLogon(serverConnectionInfo.UserID, serverConnectionInfo.Password);
string outputDestination = @"C:\Dell\Phone.pdf";
objReportDocument.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, outputDestination);
}
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.