Exception when referencing non-nullable field and even TryGetValue on various DataContainers
Was trying to retrieve from the IWebPageContentQueryDataContainer's WebPageItemParentID (int), however it throws an exception if the content item has no parent (does not default to 0, the type is int not int?).
Likewise, the TryGetValue on that columns ALSO throws an exception, vs. returning "false" as you would expect from a try catch error.
Environment
- Xperience by Kentico version: [30.0.0]
- .NET version: [8]
Answers
As much as it's an Anti-pattern, a temporary solution until this issue is resolved is to use the TryGetValue, BUT make your output variable a nullable as well. As shown in Sean's [Community Portal](community-portal/src/Kentico.Community.Portal.Web/Features/SEO/SitemapRetriever.cs at v29.7.0.4 ยท Kentico/community-portal)
int parentId = 0;
if(x.TryGetValue(nameof(WebPageFields.WebPageItemParentID), out int? parentIdVal) && parentIdVal.HasValue) {
parentId = parentIdVal.Value;
}
To answer this question, you have to login first.