Obtaining name of categories from database

Posted by Community Admin on 04-Aug-2018 11:06

Obtaining name of categories from database

All Replies

Posted by Community Admin on 04-Jun-2012 00:00

Hello!
I need to obtain all the categories names of news to put them in a list of a custom widget. I have tried to use the class CustomManager as I saw in www.sitefinity.com/.../categories-get.html So I did this:

        ContentManager content = new ContentManager();
        protected void Page_Load(object sender, EventArgs e)
        
            Guid cod = new Guid(LoadCode());
            ContentItem query = content.GetContent(cod);
        
 
              public string LoadCode()
        
            string result = "";
            path = Directory.GetCurrentDirectory().ToString() + "/Projects/sqlEmdse/App_Data/Sitefinity/Configuration/DataConfig.config";
            XDocument doc = XDocument.Load(path);
            var smt = from nodes in doc.Descendants("connectionStrings")
                      select new
                      
                          label = nodes.Element("add").Attribute("connectionString").Value,
                      ;
            foreach (var value in smt)
                conString = value.label;
            SqlConnection conn = new SqlConnection(conString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT val FROM sf_news_items_category", conn);
            result = cmd.ExecuteScalar().ToString();
            conn.Close();
            return result;
        

I used ExecuteScalar() to test the code, next time I will return an array of strings that contains all the categories.

So this code break in "ContentItem query = content.GetContent(cod);" because of a null reference. So I can´t use the existed GUID in "sf_news_items_category" table because it has no ContentItem asociated? If yes, How can I get the name of my categories by coding?

Thanks a lot.

This thread is closed