More in this section
Forums / Developing with Sitefinity / Add related Product to Dynamic Content gets error "Object must implement IConvertible"

Add related Product to Dynamic Content gets error "Object must implement IConvertible"

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
2 posts, 0 answered
  1. Nghi
    Nghi avatar
    8 posts
    Registered:
    05 Apr 2016
    24 Jun 2016
    Link to this post

    I have model like this. A CLIENT has a related data called FORMATION which based on Product. I need to add this FORMATION to related data field in CLIENT by code (not on GUI in back-end). So here the code i have done. 

    -------------------

    Guid id = new Guid();

                bool tryParse = Guid.TryParse(FormationId, out id);

                if (tryParse)
                {
                    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
                    Type clientType = TypeResolutionService.ResolveType(CpModuleClrType.Client);
                    DynamicContent clientItem = dynamicModuleManager.GetDataItems(clientType).Where(c => c.GetValue<string>(ClientField.Nom) == "name").First();

                    // This is how values for the properties are set

                   // Gets error by this line     

                    clientItem.SetValue(ClientField.Favoris, new List<Guid> { id });

                    // We can now call the following to publish the item
                    ILifecycleDataItem publishedArtistsItem = dynamicModuleManager.Lifecycle.Publish(clientItem);

                    //You need to set appropriate workflow status
                    clientItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");

                    // You need to call SaveChanges() in order for the items to be actually persisted to data store
                    dynamicModuleManager.SaveChanges();
                }

    ------------

    The Guid id is the id of a particular formation. When i run this, it keeps getting error "Object must implement IConvertible."

     

    I have tried another way fix this by replacing SetValue by CreateRelation. There was no Error but it didn't save the formation.

     

    How could i change this code to make it works?

    Thanks.

  2. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    17 Aug 2017
    04 Jul 2016
    Link to this post
    Hi,

    You may refer to the below articles from our documentation and check the samples for creating item relations: 

    http://docs.sitefinity.com/example-related-datarelated-data-api-examples
    http://docs.sitefinity.com/for-developers-related-data-api
     
    Regards,
    Sabrie Nedzhip
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
2 posts, 0 answered