Skip to content

Commit cf15805

Browse files
github-actions[bot]NansiYanchevayordan-mitev
authored
Merge kb-struct-data-error-2477 into production (#2479)
* docs(common): Add KB for null exception * Update knowledge-base/common-struct-error.md Co-authored-by: Yordan <[email protected]> * docs(common): update after review --------- Co-authored-by: NansiYancheva <[email protected]> Co-authored-by: NansiYancheva <[email protected]> Co-authored-by: Yordan <[email protected]>
1 parent 66a127f commit cf15805

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Databound Telerik Blazor Components Do Not Work and Get a Null Exception
3+
description: Using struct objects for the Data of the component causes error
4+
type: troubleshooting
5+
page_title: Struct Data Source Causes Exception
6+
slug: common-kb-struct-error
7+
tags: telerik, blazor, autocomplete, breadcrumb, chart, chiplist, combobox, contextmenu, drawer, dropdownlist, filemanager, gantt, grid, listbox, listview, menu, multicolumncombobox, multiselect, panelbar, pivotgrid, radiogroup, sankey, scheduler, stock chart, treelist, treeview, struct, null exception
8+
ticketid: 1657421
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>Autocomplete for Blazor, Breadcrumb for Blazor, Chart for Blazor, ChipList for Blazor, ComboBox for Blazor, ContextMenu for Blazor, Drawer for Blazor, DropDownList for Blazor, FileManager for Blazor, Gantt for Blazor, Grid for Blazor, ListBox for Blazor, ListView for Blazor, Menu for Blazor, MultiColumnComboBox for Blazor, MultiSelect for Blazor, PanelBar for Blazor, PivotGrid for Blazor, RadioGroup for Blazor, Sankey for Blazor, Scheduler for Blazor, Stock Chart for Blazor, TreeList for Blazor, TreeView for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
23+
## Description
24+
25+
When using a data-bound component, the application gets a null exception, and the component does not work. When running the Telerik Blazor application the application gets an error similar to the following:
26+
```
27+
ArgumentNullException: Value cannot be null. (Parameter 'source')
28+
System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
29+
```
30+
31+
To reproduce the problem, you can use the following code sample:
32+
````CSHTML
33+
<TelerikGrid Data="@GridData">
34+
<GridColumns>
35+
<GridColumn Field="@nameof(Product.Name)" />
36+
<GridColumn Field="@nameof(Product.Price)" />
37+
<GridColumn Field="@nameof(Product.Released)" />
38+
<GridColumn Field="@nameof(Product.Discontinued)" />
39+
</GridColumns>
40+
</TelerikGrid>
41+
42+
@code {
43+
private List<Product> GridData { get; set; } = new();
44+
45+
protected override void OnInitialized()
46+
{
47+
GridData = new List<Product>();
48+
49+
var rnd = new Random();
50+
51+
for (int i = 1; i <= 30; i++)
52+
{
53+
GridData.Add(new Product
54+
{
55+
Id = i,
56+
Name = "Product name " + i,
57+
Price = (decimal)(rnd.Next(1, 50) * 3.14),
58+
Released = DateTime.Now.AddDays(-rnd.Next(1, 365)).AddYears(-rnd.Next(1, 10)).Date,
59+
Discontinued = i % 5 == 0
60+
});
61+
62+
}
63+
64+
base.OnInitialized();
65+
}
66+
67+
public struct Product
68+
{
69+
public int Id { get; set; }
70+
public string Name { get; set; }
71+
public decimal Price { get; set; }
72+
public DateTime Released { get; set; }
73+
public bool Discontinued { get; set; }
74+
}
75+
}
76+
````
77+
78+
## Cause
79+
The cause for this null exception is the binding of Telerik UI for Blazor components to a `struct` component model.
80+
81+
## Solution
82+
The solution is to always bind the component to a `class` model, not a `struct`. For more information, see [Data Binding Overview]({%slug common-features-data-binding-overview%}#how-to-provide-data).
83+
84+
<div class="skip-repl"></div>
85+
86+
````CS
87+
public class Product
88+
{
89+
public int Id { get; set; }
90+
public string Name { get; set; }
91+
public decimal Price { get; set; }
92+
public DateTime Released { get; set; }
93+
public bool Discontinued { get; set; }
94+
}
95+
````
96+
97+
## See Also
98+
99+
- [Data Binding Overview]({%slug common-features-data-binding-overview%})

0 commit comments

Comments
 (0)