Mutton92 Mutton92 - 1 month ago 5
C# Question

CustomTableItemProvider.GetItem() Is returning the wrong item

I have stepped through and debugged my code and the id that is being passed to the

GetItem()
method is the ID of the training plan I am looking to get and the custom table class name is correct (It is getting the correct custom table.
However the returned item is the first item in the custom table (I've looked in the database) not the item with the ID that was entered.

int id = parameter.ID;

CustomTableItemProvider provider = new CustomTableItemProvider(CMSContext.CurrentUser);

entity = provider.GetItem(id, TrainingPlanConstants.TrainingPlanTableName);


This unrelated custom table item is returned no matter what integer value is input. I have done a work around by using the GetItems() method - but this is not ideal as I know which CustomTableItem I want and this workaround is an unnecessary loop.

(Version 7)

Answer

Strange because what you do seems correct, maybe the ID is not the ItemID in your custom table. You can debug it by looking into the database and run an query with this ID or check with the method GetItems() to see what the ItemID is.

Here is some more examples for Kentico 7 : http://devnet.kentico.com/docs/7_0/devguide/index.html?custom_tables_api_examples_managing_custom_table_data.htm

The workaround can be the GetItems methods as also is done in one the examples in the documentations. And if you use this parameters it is not so heavy for the performance :

string where = "YourIDColumn = " + id;
int topN = 1;
string columns = "ItemID";

DataSet dataSet = customTableProvider.GetItems(customTableClassName, where, null, topN, columns);

But I agree that it seems strange if you have the GetItem method and you have the ID.

Good luck with debugging and finding your solution.

Comments