Skip to Content
Former Member
Jul 09, 2013 at 09:23 PM

Error when setting Top N options a second time (via the API)


I'm trying to set the Top N options for a group using the CR .NET API. The group already has a Top N sort defined for it in the report file.

My C# code looks like this:

public void SetTopNOptions(bool top, int nGroups, bool discardOthers)


ISCRSort groupSort = reportDocument.ReportClientDocument.DataDefController.DataDefinition.Sorts[groupIndex];

TopNSort topNSort = new TopNSort();

topNSort.Direction = top ? CrSortDirectionEnum.crSortDirectionTopNOrder : CrSortDirectionEnum.crSortDirectionBottomNOrder;

topNSort.DiscardOthers = discardOthers;

topNSort.NIndividualGroups = nGroups;

topNSort.SortField = groupSort.SortField;

reportDocument.ReportClientDocument.DataDefController.SortController.Modify(groupSort, topNSort);


This succeeds the first time, and I can show the report in a preview control & it displays the correct number of groups.

However, the second time I call this on the same ReportDocument I get a NullReferenceException when I call Modify. I think this is due to the fact that groupSort.SortField is null (and therefore so is topNSort.SortField).

Interestingly, if I try to do this using the Engine classes, I get the same behaviour!

SortField sortField = reportDocument.DataDefinition.SortFields[groupIndex];

TopBottomNSortField tbnsf = sortField as TopBottomNSortField;

if (tbnsf != null)

tbnsf.NumberOfTopOrBottomNGroups = nGroups;

The first time through this code succeeds, but the second time throws a NullReferenceError. I wouldn't be surprised if the Engine-level code is doing something pretty similar to the RAS code above.

Anyway, please repro and file a bug report.


- rick cameron