New to Telerik Reporting? Download free 30-day trial

Check If Data Field Exists Before Using It

Environment

Product Progress® Telerik® Reporting

Description

In some scenarios, part of the fields used to design a report may not be available at runtime. This will cause an exception in the report stating that the corresponding field is not defined in the current scope. The error message will be displayed in a red rectangle replacing the corresponding report item. The error rectangle cannot be customized and its size cannot be controlled. This may cause additional issues in report rendering as extra horizontal page breaks that may lead to report documents with more than the expected number of pages.

Solution

Create a User Function that returns the data field when it exists, and a default value when the column is not available. Here is a sample implementation:

public static object IfFieldDoesNotExist(Telerik.Reporting.Processing.IDataObject dataObject, string fieldName, object defaultValue)
{
    object result;
    if (dataObject.TryGetValue(fieldName, out result))
    {
        return result;
    }

    return defaultValue;
}

The function receives as arguments the data object in the current data scope, the name of the field that is checked, and the default value that will be used when the field does not exist in the data source.
The function can be used in the Report like:

=IfFieldDoesNotExist(ReportItem.DataObject, "FieldNameHere", DefaultValueHere)

Details about the ReportItem.DataObject can be found in How to use the ReportItem.DataObject property in expressions article.

See Also

Error handling in Reports and Report Viewers
Extending Report Designer

In this article