More in this section

Forums / Developing with Sitefinity / Obtaining name of categories from database

Obtaining name of categories from database

1 posts, 0 answered
  1. Shil
    Shil avatar
    23 posts
    02 Apr 2012
    04 Jun 2012
    Link to this post
    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 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);
                SqlCommand cmd = new SqlCommand("SELECT val FROM sf_news_items_category", conn);
                result = cmd.ExecuteScalar().ToString();
                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.
1 posts, 0 answered