views:

262

answers:

1

I have a Crystal report that is based on a parameterized stored procedure. The report displays the value of each parameter as well as the data. In normal use, everything works well.

I now need to add some extra functionality to my reporting application, to do some extra processing on the data returned from the stored procedure. To achieve this, I want to call the procedure from my C# code, and push the data into the report, along with the parameter values.

As a proof-of-concept I am building a data table with dummy data and pushing it into the report. The parameter values do not appear in the report, but the dummy data does appear. What am I doing wrong?

I'm using Crystal Reports 11.5 to build the report, and Visual Studio 2010 to build the application. The code I'm using is:

using System;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace ReportTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var fakeTable = new DataTable("report_for_date;1");
            fakeTable.Columns.Add("Name", typeof(string));
            fakeTable.Columns.Add("Amount", typeof(decimal));

            var row = fakeTable.NewRow();
            row["Name"] = "TEST";
            row["Amount"] = 1000;

            fakeTable.Rows.Add(row);

            var rep = new ReportDocument();

            rep.Load("reportfordate.rpt", OpenReportMethod.OpenReportByDefault);

            rep.SetDataSource(fakeTable);

            rep.SetParameterValue("@date", new DateTime(2010, 05, 21) );
            rep.SetParameterValue("@threshold",5);

            // This code prints out the expected values (set above)
            foreach (ParameterField p in rep.ParameterFields)
                foreach(ParameterDiscreteValue v in p.CurrentValues)
                    Console.WriteLine("{0}={1}", p.Name, v.Value);   

            // The report PDF is generated but does not display the parameter values
            rep.ExportToDisk(ExportFormatType.PortableDocFormat,"c:/temp/hack.pdf");
        }
    }
}
A: 

Your concept should work. You should be able to gather the data in C# and then push it to the Crystal Report.

I've tried your code out and it seems to work for me. The only thing that I generally do differently is instead of using SetParameterValue, I generally build the ParameterField objects separately and then add them to a ParameterFields object and then pass that object to the report. That was is more tedious and probably not any better than this way.

Back to your issue, I was going to suggest that you make sure that the parameter names are spelled correctly (with the "@" sign), but it should give you an error if that were the case.

The only other thing would be to make sure you have the parameter fields included on the report and that they don't have any suppress logic attached to them, but that is kind of silly and I doubt that this would be the case.

All in all it looks like you're doing it right and your parameter fields should be showing up. As another test, you can always try to create a simple forms app with a crystal viewer to see what it looks like there. This would take the export part out of the mix since weirds things seem to occur in the export process sometimes.

I know this probably isn't a lot of help, but I hope you find something in this useful. :)

Dusty