blazor-docs/knowledge-base/common-stackoverflowexcepti...

2.5 KiB

title description type page_title slug tags res_type ticketid
StackOverflowException When Editing Items with Circular References in Grid for Blazor This article describes how to avoid a StackOverflowException by modifying object properties to prevent circular references when editing items in the Grid, ListView or TreeList for Blazor. troubleshooting Editing Items with Circular References in Grid for Blazor Throws StackOverflowException common-kb-stackoverflowexception-editing-circular-references telerik, blazor, grid, gantt, listview, scheduler, treelist, stackoverflowexception, circular reference, editing kb 1660663, 1592634

Environment

Product Grid for Blazor,
Gantt for Blazor,
ListView for Blazor,
Scheduler for Blazor
TreeList for Blazor

Description

I get a StackOverflowException when attempting to edit an item in the Grid for Blazor. The issue occurs when I have circular references in the data items.

Cause

The Telerik data-bound components that allow editing (for example Grid, ListView, TreeList, and more) create a copy of the original object when the user initiates editing.

The internal cloning algorithm uses reflection to loop through the object properties and copy them in the cloned object. When the model contains a self-referencing object property, the cloning logic loops again and again resulting in an infinite reflection loop and this causes the StackOverflowException exception.

Solution

To resolve the StackOverflowException, modify the data model to eliminate circular references. Replace object properties that reference other objects within the same model with primitive properties that store unique identifiers. This change prevents the infinite loop during the cloning process.

For example, instead of having a class with a property that references another object:

public class Employee
{
    public int Id { get; set; }
    public Employee Manager { get; set; }
}

Modify the class to [include a primitive property that holds a unique identifier]({%slug grids-foreign-key%}):

public class Employee
{
    public int Id { get; set; }
    public int ManagerId { get; set; }
}

See Also

  • [Grid Editing]({%slug components/grid/editing/overview%})
  • [Gantt Editing]({%slug gantt-tree-editing%})
  • [ListView Editing]({%slug listview-editing%})
  • [Scheduler Editing]({%slug scheduler-appointments-edit%})
  • [TreeList Editing]({%slug treelist-editing-overview%})