Search code examples
c#wpfcrystal-reports

Crystal Reports and WPF handling record selection and parameter


I need to integrate a Crystal Report .rpt file into my WPF application. Everything is going fine but when I added a report selection to the report it seems to wipe out the parameters. Here is the code that I am working with;

ReportClass r1 = new ReportClass();
r1.FileName = "myReport.rpt";
foreach (var t in r1.Database.Tables)
{
    CrystalDecisions.CrystalReports.Engine.Table tb = t as CrystalDecisions.CrystalReports.Engine.Table;
    tb.LogOnInfo.ConnectionInfo = conInfo;
    tb.ApplyLogOnInfo(tb.LogOnInfo);
}
CrystalDecisions.Shared.ParameterRangeValue rangeValue = new CrystalDecisions.Shared.ParameterRangeValue();
DateTime sd = (DateTime)StartDatePicker.SelectedDate;
DateTime ed = (DateTime)EndDatePicker.SelectedDate;
rangeValue.StartValue = sd;
rangeValue.EndValue = ed;
r1.SetParameterValue(r1.ParameterFields[0].Name, rangeValue);
string rs = "Correctly formated Record Section Formula"; 
r1.DataDefinition.RecordSelectionFormula = rs;
myReportViewer.ViewerCore.ReportSource = r1;

Now the record selection formula works perfectly. However it ignores the parameter value. If the r1.DataDefinition.RecordSelectionFormula = rs;is removed then the parameters work correctly. What am I doing wrong?


Solution

  • Found the answer. Turns out that r1.DataDefinition.RecordSelectionFormula = rs; was over writing the the reports record selection. Adding this fixed it

    if (rs != "")
    {
        rs = r1.DataDefinition.RecordSelectionFormula + " and " + rs;
    }
    else
    {
        rs = r1.DataDefinition.RecordSelectionFormula;
    }