|
| 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 | +When using a databound component the application gets a null exception and the component does not work. |
| 25 | + |
| 26 | +## Error Message |
| 27 | +When running a Telerik Blazor application the application gets an error similar to the following: |
| 28 | +``` |
| 29 | +ArgumentNullException: Value cannot be null. (Parameter 'source') |
| 30 | +System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) |
| 31 | +``` |
| 32 | + |
| 33 | +## Example to Reproduce |
| 34 | +````CSHTML |
| 35 | +<TelerikGrid Data="@GridData"> |
| 36 | + <GridColumns> |
| 37 | + <GridColumn Field="@nameof(Product.Name)" /> |
| 38 | + <GridColumn Field="@nameof(Product.Price)" /> |
| 39 | + <GridColumn Field="@nameof(Product.Released)" /> |
| 40 | + <GridColumn Field="@nameof(Product.Discontinued)" /> |
| 41 | + </GridColumns> |
| 42 | +</TelerikGrid> |
| 43 | +
|
| 44 | +@code { |
| 45 | + private List<Product> GridData { get; set; } = new(); |
| 46 | +
|
| 47 | + protected override void OnInitialized() |
| 48 | + { |
| 49 | + GridData = new List<Product>(); |
| 50 | +
|
| 51 | + var rnd = new Random(); |
| 52 | +
|
| 53 | + for (int i = 1; i <= 30; i++) |
| 54 | + { |
| 55 | + GridData.Add(new Product |
| 56 | + { |
| 57 | + Id = i, |
| 58 | + Name = "Product name " + i, |
| 59 | + Price = (decimal)(rnd.Next(1, 50) * 3.14), |
| 60 | + Released = DateTime.Now.AddDays(-rnd.Next(1, 365)).AddYears(-rnd.Next(1, 10)).Date, |
| 61 | + Discontinued = i % 5 == 0 |
| 62 | + }); |
| 63 | +
|
| 64 | + } |
| 65 | +
|
| 66 | + base.OnInitialized(); |
| 67 | + } |
| 68 | +
|
| 69 | + public struct Product |
| 70 | + { |
| 71 | + public int Id { get; set; } |
| 72 | + public string Name { get; set; } |
| 73 | + public decimal Price { get; set; } |
| 74 | + public DateTime Released { get; set; } |
| 75 | + public bool Discontinued { get; set; } |
| 76 | + } |
| 77 | +} |
| 78 | +```` |
| 79 | + |
| 80 | +## Possible Cause |
| 81 | +The reason for this error is because the component is databound to a `struct`. |
| 82 | + |
| 83 | +## Solution |
| 84 | +The solution is to use a `class` model, not a `struct`, as documented in the red note [here]({%slug common-features-data-binding-overview%}#how-to-provide-data): |
| 85 | + |
| 86 | +<div class="skip-repl"></div> |
| 87 | + |
| 88 | +````CS |
| 89 | + public class Product |
| 90 | + { |
| 91 | + public int Id { get; set; } |
| 92 | + public string Name { get; set; } |
| 93 | + public decimal Price { get; set; } |
| 94 | + public DateTime Released { get; set; } |
| 95 | + public bool Discontinued { get; set; } |
| 96 | + } |
| 97 | +```` |
| 98 | + |
| 99 | +## See Also |
| 100 | +- [Data Binding Overview]({%slug common-features-data-binding-overview%}) |
0 commit comments