cancel
Showing results for 
Search instead for 
Did you mean: 

Passing parameters to a report from a program

Former Member
0 Kudos

I am using Crystal Reports with Visual Studio 2017. I have written a few reports and all work well in Crystal. However I need to have the parameters validated before printing and to do this I have written a program in C#. I use a button click event to fire the report and my code is as follows:


private void btnPrint_Click(object sender, RoutedEventArgs e)
{
string reportPath = @"C:\qpaq\reports\LedegerTransactionReport.rpt";
CRLedgerTransactionReport ledgerTransactionReport = new CRLedgerTransactionReport();

ledgerTransactionReport.SetParameterValue("Start Account Number", fromAccNumber);
ledgerTransactionReport.SetParameterValue("End Account Number", toAccNumber);
ledgerTransactionReport.SetParameterValue("Start Date", fromDate);
ledgerTransactionReport.SetParameterValue("End Date", toDate);


ledgerTransactionReport.Load(reportPath);


CrystalReportsViewer crystalReportsViewer = new CrystalReportsViewer();
[crystalReportsViewer.ViewerCore.ReportSource = ledgerTransactionReport;
}


1. When I run the program I get the Crystal "Enter Parameter Values" window appear but the parameters are not populated
2. When I do a trace on the code,I can see the values entered but a trace on the last line,.ReportSource, HasRecords says
" HasRecords threw an exception of type 'CrystalDecision.CrystalReports.Engine.ParameterField(CurrentValueException"
3. I cant understand why the parameters are not being passed, not sure if there is a problem with my code.
4. The parameters above are exactly as created in the Crystal report

I would like to be able run the report from the program and not have Crystal's "Enter Parameter Values" window displayed

Your assistance is most appreciated. I thank you,
Bhuven

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

mmmmm. It works for me:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SAPBusinessObjects.WPF.Viewer;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
var reportPath = @"D:\Atest\411050\report1.rpt";

reportDocument.Load(reportPath, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);
reportDocument.SetParameterValue("End Account Number", "1");

this.MainReportViewer.ViewerCore.ReportSource = reportDocument;
}

private void MainReportViewer_OnSelectionChange(object sender, RoutedEventArgs e)
{
//this.MainReportViewer.ViewerCore.
}

private void MainReportViewer_OnDoubleClickPage(object sender, PageMouseEventArgs e)
{
//throw new NotImplementedException();
}
}
}

The only reason I can think it's prompting is because there is another parameter that needs to be updated.

Possibly the report is going out of scope, try adding some logging and try/catch to see what is happening in code.

Don

Answers (8)

Answers (8)

Former Member
0 Kudos

Hi Don

I resolved the proble, somewhere in my code i had instantiaited the viewer twice, the parameter were being passed to the second viewer.

Been looking at that code for two weeks before seeing the problem

Many thanks

Regards, bhuven

Former Member
0 Kudos

HI All,

I have been trying unsucessfully to resolve this error for the past week that I get when trying to pass parameters from my C# program to Crystal reports using C# WPF viewer. I tried it also with a report that does not have parameters and the same error occurs.. My code is above

Does anyone know where I can get access to a complete program listing just to compare idf I am missing anything in my code. I have been searching on the internet for a few days for some guidance, but cannot find anything that is complete

My reports work well if I run them directly in the viewer however, I need to validate certain parameters before printing hence the need to pass the parameters via code

Any assistance will be most appreciated, Many thanks

Bhuven

Former Member
0 Kudos

Yes I want to use the WPF Viewer. I have created the reports within Visual Studio.

For reports without parameters, I created a windows and added the viewer to the window and called the report

For these reports, I just instantiated the viewer without adding a viewer to a window.

CrystalReportsViewer crystalReportsViewer = new CrystalReportsViewer();
crystalReportsViewer.ViewerCore.ReportSource = ledgerTransactionReport;

Is this correct?

Many thanks

0 Kudos

Ah, you are using the WPF viewer, is that what you wanted to use?

Don

Former Member
0 Kudos

crerror.png

Hi Don

Here is the error m,essage, I did not upload above

thanks bhuven

Former Member
0 Kudos

Hi Don

I made the change as follows:

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
string reportPath = @"C:\qpaq\reports\LedegerTransactionReport.rpt";

CRLedgerTransactionReport ledgerTransactionReport = new CRLedgerTransactionReport();
ledgerTransactionReport.Load(reportPath);

ledgerTransactionReport.SetParameterValue("Start Account Number", fromAccNumber);
ledgerTransactionReport.SetParameterValue("End Account Number", toAccNumber);
ledgerTransactionReport.SetParameterValue("Start Date", fromDate);
ledgerTransactionReport.SetParameterValue("End Date", toDate);

CrystalReportsViewer crystalReportsViewer = new CrystalReportsViewer();
crystalReportsViewer.ViewerCore.ReportSource = ledgerTransactionReport;
}

when i run the report I get the following:

1. I did a trace and no errors detected.

2. I moved the viewer code above setparameter code, I get the same result

3. If I move the load back to below the setparameters, I get to Crystals "Entere Parameter..." window

4. I checked the path statement and moved the report as well, same error

5. Cannot get to where refernces are not set to the report

Is there anything else I can try please

Many thanks

Bhuven

Former Member
0 Kudos

Thank you Don, much appreciated, will try

0 Kudos

You need to load the report before setting parameter values

Don