Shawn de Wet
Shawn de Wet

Reputation: 5976

Crystal Reports - Replace prompts with dynamically-generated web page

I'd like to provide a reporting solution to my client that uses a number of Crystal Reports with parameters. Currently their solution uses the default mechanism of prompting the user for each parameter - this is tedious, and does not provide validation (like correct format of date entry, inter alia).

I'd like to rather present a page that accepts a report, and evaluates the parameters required by the report, and then dynamically builds a page that contains fields for each parameter, for the user to enter and then click a Run button to programmatically pass all the entered values to the report parameters.

I'm not finding anything by way of Google Search. Has this been done? I wouldn't like to re-invent the wheel on this one. Any guidance would be appreciated.

Upvotes: 0

Views: 659

Answers (1)

Shawn de Wet
Shawn de Wet

Reputation: 5976

Turns out with asp.Net MVC this was actually quite trivial... Controller:

public PartialViewResult ReportParameters(string reportFileName)
        {

            string path = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["CustomReportPath"], reportFileName);
            ReportDocument oReport = new ReportDocument();
            oReport.Load(path);
            ViewBag.ReportFileName = reportFileName;
            ViewData.Model = oReport;
            return PartialView();
        }

Razor View:

@model CrystalDecisions.CrystalReports.Engine.ReportDocument

<h2>Report: @Model.SummaryInfo.ReportTitle</h2>
@using (Html.BeginForm("PrintCustomReport", "Reporting", FormMethod.Post, new { target = "_blank" }))
{
    <input type="hidden" name="ReportFileName" value="@ViewBag.ReportFileName" />
    <fieldset>
        <legend>Enter Parameters for Report</legend>
        @{ 
    foreach (CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition oParam in Model.DataDefinition.ParameterFields)
    {
            <text>
            <div class="editor-label">
                @Html.Label(oParam.PromptText)
            </div>
            </text>
        switch (oParam.ParameterValueKind)
        {
            case CrystalDecisions.Shared.ParameterValueKind.BooleanParameter:
                    <div class="editor-field">
                        @Html.CheckBox(oParam.Name)
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.CurrencyParameter:
                    <div class="editor-field">
                    @Html.Kendo().NumericTextBox().Name(oParam.Name).Format("c")
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.DateParameter:
                    <div class="editor-field">
                    @Html.Kendo().DatePicker().Name(oParam.Name)
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.DateTimeParameter:
                    <div class="editor-field">
                    @Html.Kendo().DateTimePicker().Name(oParam.Name)
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.NumberParameter:
                    <div class="editor-field">
                    @Html.Kendo().NumericTextBox().Name(oParam.Name)
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.StringParameter:
                    <div class="editor-field">
                    @Html.TextBox(oParam.Name, oParam.HasCurrentValue ? oParam.CurrentValues[0].Description : "")
                    </div>
                break;
            case CrystalDecisions.Shared.ParameterValueKind.TimeParameter:
                    <div class="editor-field">
                    @Html.Kendo().TimePicker().Name(oParam.Name)
                    </div>
                break;
            default:
                break;
        }
    }
        }
    </fieldset>
    <input class="k-button" onclick="CloseWindow()" type="submit" value="Print" />
    <input  class="k-button" onclick="CloseWindow()" type="button" value="Cancel" />
}

The submit button would then Post to a PrintCustomReport method on the controller, which returns a PdfReportResult, as described by @RenanMarks here

Upvotes: 0

Related Questions