Can't retrieve data from custom table

giang le asked on May 8, 2014 09:13

Hi all! I tried retrieve data from custom table follow code Get all items

          // Create new Custom table item provider

        var customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);

        string customTableClassName = "xxx";

        // Check if Custom table 'Sample table' exists

        DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);

        if (customTable != null)
        {

            // Prepare the parameters   

            DataSet customTableItems = customTableProvider.GetItems(customTableClassName, null, null);


        }

    }

Get item

`// Create new Custom table item provider`
            var customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);

            string customTableClassName = "xxxx";

            // Check if Custom table 'Sample table' exists

            DataClassInfo customTable = DataClassInfoProvider.GetDataClass(customTableClassName);

            if (customTable != null)
            {

                // Prepare the parameters   

                string where = "ItemID = 1";
                // Get the data

                DataSet customTableItems = customTableProvider.GetItems(customTableClassName, where, null);

                if (!DataHelper.DataSourceIsEmpty(customTableItems))
                {
                    return true;
                }
            }
            return false;

but it's not working!

Please help meeeeeeeee!

GiangLT3

Recent Answers


Brenden Kehren answered on May 8, 2014 10:48

You don't need to create a new instance of CustomTableItemProvider. Simply use the static method: DataSet customTableItems = CustomTableItemProvider.GetItems(className, whereClause, orderBy, topN);

0 votesVote for this answer Mark as a Correct answer

giang le answered on May 8, 2014 22:12

Hi Brenden Kehren! I tried your suggest but this method isn't static function so i can't using it. ps: I using Kentico 7.x

Thank you!

GiangLT3

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 9, 2014 06:31

Ok, that was for v8. Only thing I can see is if someone isn't logged in the CMSContext.CurrentUser will be null and you won't be able to get your new instance of the CustomTableItemProvider. I should also ask, what isn't working? Are you getting an error message?

0 votesVote for this answer Mark as a Correct answer

giang le answered on May 12, 2014 02:23

Thank for your reply! I didn't get an error message but result always is null object. However, I sure there are records in table that i want query. When I debug in my code, I got CMSContext.CurrentUser is object have full name is "Public Anonymous User" and it isn't null.

I tried uses ConnectionHelper.ExecuteQuery(querySql, new QueryDataParameters(), QueryTypeEnum.SQLQuery,false);

but it do not work correctly.

Do you have other way to retrieve data?

Thank! GiangLT3

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 12, 2014 10:00

Use a custom User. The public If the user isn't authenticated then set it to someone in your User list that has permissions to access that table.

0 votesVote for this answer Mark as a Correct answer

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