Cannot retrieve data from CustomTableItemProvider.GetItems kentico

Dung Vo asked on September 25, 2018 11:28

Hi all!

I am using Kentico 11, and I want to get data from custom table.

string tableName = "customtable.MenuNavigation";
    DataClassInfo imageTable = DataClassInfoProvider.GetDataClassInfo(tableName);
    if (imageTable != null)
    {

        DataTable dtMenu = CustomTableItemProvider.GetItems(tableName).Where("MenuType='menu'").Tables[0];
        rptMenu.DataSource = dtMenu;
        rptMenu.DataBind();
    }

Last time, It still worked perfectly. But now the object CustomtableItemProvider.GetItems cannot return any data.

I have to change my code to using dataset like this:

DataTable dtMenu = ConnectionHelper.ExecuteQuery("SELECT * FROM customtable_MenuNavigation WHERE MenuType = 'menu'", null, QueryTypeEnum.SQLQuery).Tables[0];

It works, but I don't really know why this happen.

Thanks you.

Recent Answers


David te Kloese answered on September 25, 2018 11:44

I guess you've checked the customtable code name still is customtable.MenuNavigation_YNV?

did you do any upgrades/hotfix installs?

Is there anything changed on the custom table? Fields? Assigned site?

0 votesVote for this answer Mark as a Correct answer

Dung Vo answered on September 25, 2018 11:56

Thanks for you reply.

I have checked so many times and the customable code name still right.

And FYI, all the code used by: CustomTableItemProvider.GetItems(tableName) return null and I can't find the reason.

Thanks

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 25, 2018 14:01

Do you have data in the table?

0 votesVote for this answer Mark as a Correct answer

Dung Vo answered on September 26, 2018 04:48 (last edited on September 26, 2018 04:50)

of course it does have a data.

When I use ConnectionHelper and select to table. It returns a dataset contain data. But when I use CustomTableItemProvider.getItems. It return null.(I already metioned that It worked before but now I can not use CustomTableItemProvider.getItems)

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 26, 2018 05:09

When you use the api, why are you casting to a table? Why not just use the object query and assign to the repeater? Assuming you're doing this in a custom webpart, if you do it in the Init event, it will automatially bind the datasource to the repeater so no need to call it. These 2 lines should be all you need.

var dtMenu = CMS.CustomTables.CustomTableItemProvider.GetItems("ClassName").Where("MenuType='menu'");
rptMenu.DataSource = dtMenu;

Lastly, I'd question the need for custom code when you can simply use a custom table repeater with a simple where condition to accomplish this task (unless you have other code being run).

0 votesVote for this answer Mark as a Correct answer

Dung Vo answered on September 26, 2018 05:40

It works, But I still do not understand.

Last time when I use like you, and I want to for each item:

 var dtMenu = CustomTableItemProvider.GetItems(tableName).Where("MenuType='menu'");
        foreach (var item in dtMenu)
        {
            var b = item["MenuName"];
        }

The table has a data but It could not get in to the foreach loop.

Another project I still use like this, and I could get in the loop

But now I couldn't

Could you explain it for me?

Thanks you.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 26, 2018 06:54

Depending on when you assign the data source the data table may not be filled yet. Kentico has it's own life cycle and when creating webparts you want to stick to assigning your property values in the overloaded method below. The rest will happen on its own.

public override void OnContentLoaded()
{
    base.OnContentLoaded();
    // Do your work here, assign property values
}

I can guarantee, your issue has everything to do with page life cycle

0 votesVote for this answer Mark as a Correct answer

Dung Vo answered on September 26, 2018 08:49

Im trying to do as you said but seem its not working.

I just dont know that, same code but now I couldn't run and I have to change to use dataset instead of CustomeTable Object

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 26, 2018 13:39 (last edited on September 26, 2018 13:40)

This block of code:

var dtMenu = CustomTableItemProvider.GetItems(tableName).Where("MenuType='menu'");
foreach (var item in dtMenu)
{
    var b = item["MenuName"];
}

Should look like this:

var dtMenu = CustomTableItemProvider.GetItems(tableName).Where("MenuType='menu'");
string s = "";
foreach (var item in dtMenu)
{
    s += item.GetValue("MenuName" "");
}
0 votesVote for this answer Mark as a Correct answer

Dung Vo answered on September 27, 2018 06:21

I think you dont understand my problem.

The problem is I can't get into the loop although I have a data in my custom table.

And I want to mention one more time that this method still work fine 2 weeks ago. But now it could't

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 27, 2018 07:11

Well you have confused me because you stated it didn't work and then the code I posted did work. So let's back up.

What does your data look like? Show some sample data.

Secondly, is any API call returning data from a customer table?

Third, have you stepped through the debugging process to see that the result is filled?

0 votesVote for this answer Mark as a Correct answer

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