title | description | type | page_title | slug | position | tags | ticketid | res_type |
---|---|---|---|---|---|---|---|---|
Check If Data Field Exists Before Using It |
How to avoid Report error when data field does not exist |
how-to |
Avoid Report error when no such data field |
check-if-data-field-exists-before-using-it |
1462533 |
kb |
Product | Progress® Telerik® Reporting |
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.
Create a [User Function]({%slug telerikreporting/designing-reports/connecting-to-data/expressions/extending-expressions/user-functions%}) 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]({%slug telerikreporting/designing-reports/connecting-to-data/data-items/how-to-use-the-reportitem.dataobject-property-in-expressions%}) article.
Error handling in Reports and Report Viewers
[Extending Report Designer]({%slug telerikreporting/designing-reports/report-designer-tools/desktop-designers/standalone-report-designer/configuration/extending-report-designer%})