More in this section

Forums / Developing with Sitefinity / The object belongs already into a different transaction

The object belongs already into a different transaction

7 posts, 0 answered
  1. Nigel
    Nigel avatar
    49 posts
    Registered:
    07 Jul 2008
    25 Jan 2010
    Link to this post
    Hi, I am writing a simple method to bring in photos from an old CMS system into Sitefinity, however I have run into a problem.

    I am using the following code:
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);  
            LibraryManager man = new LibraryManager();  
            ILibrary lib = man.GetLibrary("PhotoGallery");  
            int i = 0;  
            foreach (DataRow row in dv.Table.Rows)  
            {  
                IContent item = man.UploadFile((byte[])row["Picture"], "Item" + i, ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true);  
                if(row["Description"] != DBNull.Value)  
                    item.SetMetaData("Description", (string)row["Description"]);  
                if (row["Category"] != DBNull.Value)  
                    item.SetMetaData("Category", (string)row["Category"]);  
                item.SetMetaData("Author", "Site Importer");  
                man.SaveContent(item);  
            } 

    However on the second upload I get the following error: The object belongs already into a different transaction from Nolnet.

    which part of the code is causing my issues, the line that is throwing the exception is IContent item = man.UploadFile((byte[])row["Picture"], "Item" + i, ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true);  

    Regards
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Jan 2010
    Link to this post
    Hi Nigel,

    You need to increment the counter in the end of foreach loop

    foreach (DataRow row in dv.Table.Rows) 
            
                IContent item = man.UploadFile((byte[])row["Picture"], "Item" + i, ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true); 
                if(row["Description"] != DBNull.Value) 
                    item.SetMetaData("Description", (string)row["Description"]); 
                if (row["Category"] != DBNull.Value) 
                    item.SetMetaData("Category", (string)row["Category"]); 
                item.SetMetaData("Author", "Site Importer"); 
                man.SaveContent(item); 
             i++;
            }


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Nigel
    Nigel avatar
    49 posts
    Registered:
    07 Jul 2008
    27 Jan 2010
    Link to this post
    Yes, that did seem to be the problem, sorry should have seen that. If this forum supported emotes insert embrassed emote here.

    However I have a further problem, I am saving the description and category of the item as you can see, however when you look at the images in the image and documents module there is no information in these fields saved.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Jan 2010
    Link to this post
    Hello Nigel,

    Does the author get saved? It is also a metakey. Make sure that the Description and Category metakeys are not null. You can get the content item right after you call man.SaveContent(item) and use GetMetaData to see what is saved.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Nigel
    Nigel avatar
    49 posts
    Registered:
    07 Jul 2008
    27 Jan 2010
    Link to this post
    Hi Ivan, no, not even the author is being saved. I have steped through the code and made sure that description and category is not null. Category will never be null from the old system.

    The method now looks like this:
    DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);  
            int i = 0;  
            LibraryManager man = new LibraryManager();  
            ILibrary lib = man.GetLibrary("PhotoGallery");  
            foreach (DataRow row in dv.Table.Rows)  
            {  
                IContent item = man.UploadFile((byte[])row["Picture"], "Item - " + i.ToString().PadLeft(4, '0'), ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true);  
                if (row["Description"] != DBNull.Value)  
                {  
                    string description = (string)row["Description"];  
                    description = description.Replace("\r\n", "");  
                    description = description.Replace("\n\r", "");  
                    string altText = Regex.Replace(description, "<.*?>", string.Empty);  
                    if (altText.Length > 245)  
                        altText = altText.Substring(0, 245);  
                    description = description.Replace("<br/>", "\r\n");  
                    description = description.Replace("<br />", "\r\n");  
                    description = description.Replace("<BR />", "\r\n");  
                    description = description.Replace("<br>", "\r\n");  
                    description = description.Replace("<BR>", "\r\n");  
                    description = Regex.Replace(description, "<.*?>", string.Empty);  
                    item.SetMetaData("AlternateText", altText);  
                    item.SetMetaData("Description", description);  
                }  
                if (row["Category"] != DBNull.Value)  
                    item.SetMetaData("Category", (string)row["Category"]);  
                item.SetMetaData("Author", "Site Importer");  
                man.SaveContent(item); //, ContentStatus.Published);  
     
                item = man.GetContent(item.ID);  
     
                i++;  
                  
            }
            

    With watches applied to view the meta data on the item. Author, Category, AlternateText and Description seems to be saving correctly at this level. However when you go and view the images through the image and document module all fields are still blank.

    On Further investigation, looking at the raw data in sf_GCMetaData table (where I can see other entered values for /Libraries items) there are rows for these items, and the Name, Extension are saving correctly (from the uploadFile method), however none of the values from the SetMetaData(...) and man.SaveContent(...) seem to being saved. Is it possible that the transaction associated with these changes are never being committed?

    Just so you know to save a day and a question, I am running 3.7 SP2.
  6. Nigel
    Nigel avatar
    49 posts
    Registered:
    07 Jul 2008
    28 Jan 2010
    Link to this post
    The following method worked:
    DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);  
            int i = 0;  
            LibraryManager man = new LibraryManager();  
            ILibrary lib = man.GetLibrary("PhotoGallery");  
            foreach (DataRow row in dv.Table.Rows)  
            {  
                IContent item = man.UploadFile((byte[])row["Picture"], "Item - " + i.ToString().PadLeft(4, '0'), ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true);  
                IContent item2 = man.GetContent(item.ID);  
                if (row["Description"] != DBNull.Value)  
                {  
                    string description = (string)row["Description"];  
                    descriptiondescription = description.Replace("\r\n", "");  
                    descriptiondescription = description.Replace("\n\r", "");  
                    string altText = Regex.Replace(description, "<.*?>", string.Empty);  
                    if (altText.Length > 245)  
                        altTextaltText = altText.Substring(0, 245);  
                    descriptiondescription = description.Replace("<br/>", "\r\n");  
                    descriptiondescription = description.Replace("<br />", "\r\n");  
                    descriptiondescription = description.Replace("<BR />", "\r\n");  
                    descriptiondescription = description.Replace("<br>", "\r\n");  
                    descriptiondescription = description.Replace("<BR>", "\r\n");  
                    description = Regex.Replace(description, "<.*?>", string.Empty);  
                    item2.SetMetaData("AlternateText", altText);  
                    item2.SetMetaData("Description", description);  
                }  
                if (row["Category"] != DBNull.Value)  
                    item2.SetMetaData("Category", (string)row["Category"]);  
                item.SetMetaData("Author", "Site Importer");  
                man.SaveContent(item2); //, ContentStatus.Published);  
                 
                Literal1.Text += "Saved item " + i + " - " + item2.GetMetaData("Description") + "<br/>";  
                  
     
                i++;  
                  
            } 

    The change that made the difference was after running

     


    IContent item = man.UploadFile((byte[])row["Picture"], "Item - " + i.ToString().PadLeft(4, '0'), ".jpg", (string)row["PictureType"], (int)row["PictureSize"], lib, true); 

    I then loaded the content item back out of the database

     

     

     

     

    IContent item2 = man.GetContent(item.ID) 
    and assigned the metadata to this instance of the item, rather than the one returned by the UploadFile method
    item2.SetMetaData("AlternateText", altText);  
                    item2.SetMetaData("Description", description);  
                }  
                if (row["Category"] != DBNull.Value)  
                    item2.SetMetaData("Category", (string)row["Category"]);  
                item.SetMetaData("Author", "Site Importer");  
                man.SaveContent(item2); //, ContentStatus.Published); 

    Hope this helps any one else that comes across this. When uploading a file, do not use the IContent that is returned by the UploadFile method to apply your MetaData. Make sure you fetch it again using the GetContent method

    There is still one mystery however. When I check the Database the content is now there, and when I view the ImageGallery component it is showing the descriptions etc, however if I go into the Image and Document module and edit an image these metadata fields are not loading and are showing as blank, but the information is showing in the view tab? So I can view this information but not edit it.. strange....
    Regards

     

     

     

     

  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 Jan 2010
    Link to this post
    Hi Nigel,

    To set the metadata you can just get the content item right after you save the uploaded file. This should persists the metadata.

    sample

    .....
         .....
     
    IContent item = man.UploadFile(param1, param2 + i, param3, param4, param5, param6, true);
    man.SaveContent(item);    
     
    i++;
    // get the content again as staged content
    item = man.GetContent(item.ID);
    StagedContent stg = man.GetStagedContent(item.ID) as StagedContent;
    stg.SetMetaData("Author", "SiteImporter");
    man.SaveContent(stg);


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
7 posts, 0 answered