General suggestion about how to handle datasource of controls

I take a specific issue that I had upgrading to 26.x with TreeView for a general question

I had this code that was running fine since 6 months (v23/24/25)

<SfTreeView TValue="TreeData">
<TreeViewFieldsSettings TValue="TreeData" DataSource="@Data"
Id="Id" Text="Name" ParentID="ParentId" HasChildren="HasChildren" Expanded="true"></TreeViewFieldsSettings>
</SfTreeView>

@code
{
    public List<TreeData> Data { get; set; }
    protected override async Task OnInitializedAsync()
    {
       TreeData = await MyService.LoadData(); //could be a lazy and slow
    }
}

With 26.1.35 it crashes with this error

System.ArgumentNullException: Value cannot be null. (Parameter 'other')
   at System.Collections.Generic.HashSet`1.SetEquals(IEnumerable`1 other)
   at Syncfusion.Blazor.Navigations.TreeViewFieldsSettings`1.OnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)

The workaround I found is one (or both) of these:

public List<TreeData> Data { get; set; } = new();

or surround the control with an isnull check of data

@if (Data is not null)
{
<SfTreeView TValue="TreeData">
...
</SfTreeView>
}

General speaking what do you advice? I was guessing all of your controls (like the grid) are checking if the data is null, but maybe I'm wrong...

Waiting from a general behaviour of how to handle your control bound to a datasource...


3 Replies

LD LeoLavanya Dhanaraj Syncfusion Team June 20, 2024 10:01 AM UTC

Hi Sandro,


Greetings from Syncfusion support.


From the shared details, we understand that you are facing a console error issue(Value cannot be null. (Parameter 'other')) with the TreeView component in the latest version (26.1.35). Based on the shared code snippets, we have prepared and validated the TreeView sample with .NET 8 framework and the latest NuGet packages. We are trying to replicate the issue by loading data after a time delay as well as a null value to the TreeView data source. However, we were not able to replicate the issue on our end. For your reference, we have included the validated sample.


Please check our shared sample on your end and try to replicate the issue in our shared sample. If issue persists, share the replication steps or a video footage. This information will enable us to better understand your issue and provide an appropriate solution.


Regards,

Leo Lavanya Dhanaraj


Attachment: BlazorApp_6b1eac08.zip


SR Sandro Rizzetto June 21, 2024 12:30 PM UTC

I see... thanks for your sample. I really don't know what could cause the error. I rechecked all my project and without one of the 2 corrections (the new() of the list or the if not null befor the control) I got the exception, see attach.


Anyway, aside this particular case, what is your "official" and general advice?


always initializing as new() the IEnumerable collections that are datasources ? 

Checking always if they are null?

None of these?

Thanks




Attachment: 20240621_142213_85c05fa.zip


LD LeoLavanya Dhanaraj Syncfusion Team June 24, 2024 09:23 AM UTC

Hi Sandro,


Based on the information provided, we have also checked the error issue with and without the mentioned two correction details on our end (the new() of the list or the if not null before the control). However, we were not able to reproduce it on our end.


We would like to inform you that the ArgumentNullException: value cannot be null (parameter 'other') exception occurs in .NET applications when a method is called with a null argument for a parameter that expects a non-null value. Here are some common reasons why this exception might be thrown:


1. Null Argument Passed to a Method: A method that doesn't accept null arguments.

2. Invalid Initialization: An object that should be initialized is null.

3. Null Property or Field: Accessing a property or field that hasn't been properly initialized.

4. Null in Collection or Enumerable: Performing operations on collections or enumerables with null elements.


Please follow the above details to check and overcome this issue. If the issue still persists, share the code details of the CreateNodeAsync and LoadNodes methods that has been used on your end. Also, once trace the execution and identify where the null value is coming from in your application.


Loader.
Up arrow icon