+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / News Catagories

News Catagories

2 posts, 0 answered
  1. Asif
    Asif avatar
    110 posts
    Registered:
    18 Nov 2010
    08 Dec 2010
    Link to this post
    Hi,
    I am getting following error when I try to show News categories with count.

    Unable to cast object of type 'Telerik.Cms.Engine.Data.Category' to type 'Telerik.Cms.Engine.IContent'.

    My code is

    private IList GetCategoryPosts()
        {
            ContentManager Manager = new ContentManager("News");
            IList posts = Manager.GetCategories();

            List<ICategory> results = new List<ICategory>();

            foreach (IContent post in posts)
            {
                string catName = post.GetMetaData("Category").ToString();
                if (catName == null || catName == string.Empty)
                    continue;

                Telerik.Cms.Engine.ICategory getCategoryForPRovider = (ICategory)Manager.GetCategory(catName);
                if (results.IndexOf(getCategoryForPRovider) < 0)
                {
                    results.Add(getCategoryForPRovider);
                }
            }

            return results;
        }

        private int CountPosts(ICategory category)
        {
            ContentManager Manager = new ContentManager("News");
            IList posts = Manager.GetCategories();
            int numPosts = 0;

            foreach (IContent post in posts)
            {
                string catName = post.GetMetaData("Category").ToString();
                if (catName != null && catName != string.Empty && catName == category.CategoryName)
                {
                    numPosts++;
                }
            }

            return numPosts;
        }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Dec 2010
    Link to this post
    Hi Asif,

    The problem is in the code below

    IList posts = Manager.GetCategories();
            int numPosts = 0;

            foreach (IContent post in posts)

    GetCategories(); does not return list of IContent objects. It returns a list of ICategory objects.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered