Navigation property throws null reference exception

Hello,
I am using Grid control to display product record with navigation property Category.
I implemented ProductService to retrieve the records to be displayed by the grid. But the navigation property Category always throw Null Reference Exception even though the navigation property is not null.
Please find relevant sections of my code.
//productservice class
public class ProductListBase : ComponentBase
{
[Inject]
public ICategoryService CategoryService { get; set; }
[Inject]
public IProductService ProductService { get; set; }
public ProductForList product { get; set; } = new ProductForList();
public List Products = new List();
[Parameter]
public string Id { get; set; }
public bool isSelected { get; set; } = false;
public string DisableMenuCssClass = "disabledMenu";
public string DisableMenuCssClassOnEmptyList = "disabledMenu";
[Parameter]
public string SelectedPatientId { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
}
protected async override Task OnInitializedAsync()
{
Products = (await ProductService.GetProductsWithRel()).ToList();
}
//product view
AllowPaging="true"
AllowSorting="true"
AllowResizing="true"
AllowReordering="true"
AllowFiltering="false"
Toolbar="@(new List() { "Search" })">
HeaderText="Product Code #" TextAlign="@TextAlign.Right"
Width="70">
HeaderText="Product Name" TextAlign="@TextAlign.Left"
Width="80">
this is the line that throws an exception
HeaderText="Category" TextAlign="@TextAlign.Left"
Width="80">
HeaderText="Unit of Sale" TextAlign="@TextAlign.Left"
Width="80">
HeaderText="Unit Price" TextAlign="@TextAlign.Right"
Format="N2"
Width="80">
HeaderText="Qty Per Unit" TextAlign="@TextAlign.Left"
Format="N2"
Width="80">
Thanks in advance for your usual assistance

9 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team March 5, 2021 02:07 PM UTC

Hi Paul, 

Greetings from Syncfusion. 

We have validated your query and we suspect that you are facing problem while binding complex data. You can achieve complex data binding in the datagrid by using the dot(.) operator in the column.field. In the below examples Name.FirstName and Name.LastName are complex data. 

 
<SfGrid DataSource="@Employees" Height="315"> 
    <GridColumns> 
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="Name.FirstName" HeaderText="First Name" Width="150"></GridColumn> 
        <GridColumn Field="Name.LastName" HeaderText="Last Name" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<EmployeeData> Employees { get; set; } 
 
    . . . 
 
    public class EmployeeData 
    { 
        public int? EmployeeID { get; set; } 
        public EmployeeName Name { get; set; } 
        public string Title { get; set; } 
    } 
 
    public class EmployeeName 
    { 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    } 
} 

Reference: 

If you are still facing the problem or if we misunderstood your problem, then could you please share the below details. It will be helpful to validate and provide a better solution. 

  • Full Grid code snippets.
  • Share your model class details.
  • Share a simple reproduceable sample if possible.

Regards, 
Rahul 



PA Paul replied to Rahul Narayanasamy March 26, 2021 01:21 PM UTC

Hi Paul, 

Greetings from Syncfusion. 

We have validated your query and we suspect that you are facing problem while binding complex data. You can achieve complex data binding in the datagrid by using the dot(.) operator in the column.field. In the below examples Name.FirstName and Name.LastName are complex data. 

 
<SfGrid DataSource="@Employees" Height="315"> 
    <GridColumns> 
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="Name.FirstName" HeaderText="First Name" Width="150"></GridColumn> 
        <GridColumn Field="Name.LastName" HeaderText="Last Name" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<EmployeeData> Employees { get; set; } 
 
    . . . 
 
    public class EmployeeData 
    { 
        public int? EmployeeID { get; set; } 
        public EmployeeName Name { get; set; } 
        public string Title { get; set; } 
    } 
 
    public class EmployeeName 
    { 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    } 
} 

Reference: 

If you are still facing the problem or if we misunderstood your problem, then could you please share the below details. It will be helpful to validate and provide a better solution. 

  • Full Grid code snippets.
  • Share your model class details.
  • Share a simple reproduceable sample if possible.

Regards, 
Rahul 


Thanks for suggestion but I am still facing the problem. Sorry for the late response. I hade to put the project on hold for sometime. 


Please find attached my class model and related files you might find useful. Should you require additional info, please let me know

Attachment: syncfusion_blazor_71b7a0c0.rar


RN Rahul Narayanasamy Syncfusion Team March 29, 2021 01:03 PM UTC

Hi Paul, 

Thanks for sharing the details. 

We have validated your query with the provided information and we suggest you to define the complex data by using dot(.) operator as mentioned in our previous update. Find the below code snippets for your reference. 

Reference: 

[ServiceList.razor] 
<SfGrid DataSource="@services" 
        AllowPaging="true" 
        . . .> 
    <GridPageSettings PageSize="20"></GridPageSettings> 
    <GridEvents RowDeselected="RowDeSelectedHandler" RowSelected="RowSelectedHandler" TValue="ServiceForListDto"></GridEvents> 
    <GridColumns> 
        <GridColumn IsPrimaryKey="true" Field=@nameof(service.ProductCode) 
                    HeaderText="Service Code #" TextAlign="@TextAlign.Right" 
                    Width="70"> 
        </GridColumn> 
        <GridColumn Field=@nameof(service.ProductName) 
                    HeaderText="Service Name" TextAlign="@TextAlign.Left" 
                    Width="80"> 
        </GridColumn> 
        <GridColumn Field="Category.Name" 
                    HeaderText="Category" TextAlign="@TextAlign.Left" 
                    Width="80">    // define Category.Name instead of Field=@nameof(service.Category.Name) 
        </GridColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 

If you are still facing the problem, then could you please share a simple reproduceable sample. It will be helpful to validate and provide a better solution.  

Regards, 
Rahul 


Marked as answer

PA Paul March 30, 2021 09:54 PM UTC

Hello Rahul,

Thanks for your suggestion. Your suggestion works.


Thanks for your assistance.


RN Rahul Narayanasamy Syncfusion Team March 31, 2021 06:44 AM UTC

Hi Paul, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful to achieve your requirement. 
 
Please get back to us if you need further assistance. 
 
Regards, 
Rahul 



PA Paul April 17, 2021 04:34 PM UTC

Hello Rahul, 

There is mistakenly said your suggestion worked. 


I was wrong. The problem persist so I have in the attached file all the relevant classes to help you. 

Attachment: file_2f3c4b5c.rar


RN Rahul Narayanasamy Syncfusion Team April 19, 2021 12:52 PM UTC

Hi Paul, 

Thanks for the update. 

We have validated your query with the provided information and you have defined Field Name as “CategoryForList.Name” instead of “Category.Name”. Could you please ensure the reported problem after changing the Field value as below. 

 <GridColumn Field="Category.Name"  
                    HeaderText="Category" TextAlign="@TextAlign.Left"  
                    Width="80">    // define Category.Name instead of Field=”CategoryForList.Name”  
        </GridColumn>  

Reference

Please let us know if you have any concerns. 

Regards, 
Rahul 



PA Paul April 19, 2021 02:38 PM UTC

Thanks so much for the prompt response.

'Category.Name' worked like magic. 


Thanks once again for your patience


RN Rahul Narayanasamy Syncfusion Team April 20, 2021 04:26 AM UTC

Hi Paul, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful to resolve your problem. Please get back to us if you need further assistance. 
 
Regards, 
Rahul 


Loader.
Up arrow icon