More in this section

Forums / Developing with Sitefinity / Staged.AddControl at Specific Ordinal: "Primary key cannot be null"

Staged.AddControl at Specific Ordinal: "Primary key cannot be null"

5 posts, 0 answered
  1. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    15 Jul 2010
    Link to this post
    Good Evening,

    I'm trying to add a new control at a specific index in the control collection of a staged page. The way I'm doing it causes Sitefinity to throw an exception: "Primary key cannot be null."

    Can you look at my code and see what I'm doing wrong or suggest another way to accomplish this?

    Thanks,
    Jeff

    CmsManager Manager = new CmsManager();
    ICmsPage ThePage = (ICmsPage)Manager.GetPage(new Guid(TextBox2.Text), true);
     
    if (ThePage.Staged.IsCheckedOut)
    {
        IStagedPage staged = ThePage.Staged;
     
        CmsWebControl NewControl = new CmsWebControl(new
            LiteralControl("<p>Stuck in the middle with you</p>"));
     
        // Exception: Primary key cannot be null
        staged.AddControl("Content", NewControl, 2);
     
        Manager.SavePage(ThePage);
    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    15 Jul 2010
    Link to this post
    Hello Jeff,

    The error is thrown because you need to set the control ID which is a PrimaryKey. Also adding controls at run time will not work if you use this overload, because the object is not in a transaction.
    You can use

    AddControl(string containerId, string url, int ordinal, string controlId)

    or

    AddControl(string containerId, Type type, string controlId)


    Best wishes,
    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
  3. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    15 Jul 2010
    Link to this post
    I need an overload of AddControl that takes an ordinal and accepts a control with a CompositeControl base class. There is no ascx file associated with the controls I'm adding.

    The 8th overload takes an ordinal and a controlID. Is there a way to use that overload for what I'm trying to do?

    It's not clear to me how to Sitefinity would associate the controlID to an initialized control.
  4. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    15 Jul 2010
    Link to this post
    I see what you mean about the object not being in a transaction.

    If I assign an ID to the CMSWebControl, the AddControl call succeeds. However, the Staged.Controls collection throws an exception when accessed thereafter.

    Are there some Nolics calls I can use to put the object in a transaction?

    Thanks,
    Jeff
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    16 Jul 2010
    Link to this post
    Hello Jeff,

    You can use this overload AddControl(string containerId, Type type, int ordinal, string controlId) which will put the object into transaction.

    Another option is using the code below

    var Manager = new CmsManager();
           ICmsPage p = (ICmsPage)Manager.GetPage(new Guid("9327e34e-62b9-47b2-bcab-f8a0de073922"), true);
           IStagedPage staged = p.Staged.CheckOut();
           Transaction tran = ((IOdbClass)staged).CurrentTransaction;
           if (tran == null)
           {
               throw new InvalidOperationException("ex");
           }
           GenericContent cnt = new GenericContent();
           CmsWebControl cmsCtrl = new CmsWebControl(cnt);
           cmsCtrl.LoadControl().ID = "someID";
     
     
           tran.Create(cmsCtrl, ((IOdbClass)staged).Provider);
           staged.AddControl("Footer", cmsCtrl, 0);
     
           p = (ICmsPage)Manager.GetPage(p.ID, true);
           p.Staged.CheckIn();
           p = (ICmsPage)Manager.GetPage(p.ID, true);
           Manager.SavePage(p);

    but this is a hack.

    Kind regards,
    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
Register for webinar
5 posts, 0 answered