Custom Table Data

Austin Harmon asked on May 5, 2020 22:54

Hello So I am working with Kentico 10.

Here is the code I am using:

string customTableClassName = "customtable.activity_variants";
CustomTableItem item = CustomTableItemProvider.GetItems(customTableClassName).WhereEquals("ActivityVariant", allActs).TopN(1).FirstOrDefault();
string productCode = ValidationHelper.GetString(item.GetStringValue("ProductCode", ""), "")

So For some context I am taking in a comma delmited list of values and splitting them then iterating through the list to look up the activity data in the cutom table by the "Activity Variant" Name so that is what allActs contains. Then I am looking my custom field of ProductCode which does exist in the custom table config as well as in the database. But when I run this I get an error saying there is a null reference. Like ProductCode doesn't exist in the table.

Here is the error I'm getting:

Message: Object reference not set to an instance of an object.

Exception type: System.NullReferenceException

and it happens on the line that I am calling the custom field.

If this looks correct then Is there a way to determine if the item has come back without check for null, I didn't see a result code or any kind of way other than calling custom fields to determine if the custom table item was returned sucessfully.

Thank you, Austin

Recent Answers

Peter Mogilnitski answered on May 5, 2020 23:50

You may produce redundant queries. allActs is comma delimited list? Check WhereIn mtehod. CustomTableItemProvider.GetItems(customTableClassName).WhereIn(string columnName, List<string> yourList) You also need to check if CustomTableItem item is not null and then access data.

0 votesVote for this answer Mark as a Correct answer

Austin Harmon answered on May 19, 2020 17:02

This worked. Thank you!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.