Tip: Get report parameters as text from Crystal Reports in C#

I am starting a new set of posts presenting small code snippets in C# you could use when working with Crystal Reports in Visual Studio.

And the first one is “GetReportParamsAsText“. Very simple but very handy function which allows to include report parameters values in your ASP.Net page without actually displaying the report.
This version works only with Discrete Value parameters. 

/// Function returns list of paramaters descriptions with
/// associated values for specified report
public string GetReportParamsAsText(ReportDocument reportDocument)
{
    if (reportDocument == null) return "Report not specified";
    string result = "";
    string description;
    string valueText;
    ParameterDiscreteValue value;
    foreach (ParameterFieldDefinition prm
                  in reportDocument.DataDefinition.ParameterFields)
    {
        try
        {
            if (prm.CurrentValues.IsNoValue) continue;
            if (prm.CurrentValues[0] is ParameterDiscreteValue)
            {
                value = (ParameterDiscreteValue)prm.CurrentValues[0];
                if (description.StartsWith("Enter "))
                {
                    description = description.Remove(0, 6);
                }
                valueText = value.Value.ToString();
            }
            else
            {
                valueText = "[not supported param type]";
            }
            result += "<strong>" + description + "</strong> " + valueText + "
";
        }
        catch
        {
            // Ignore any errors
        }
    }
    return result;
}
This entry was posted in Crystal Reports and tagged , , . Bookmark the permalink.

Leave a Reply