Developer forum

Forum » Development » Product.Save() gives error on some languages.

Product.Save() gives error on some languages.

Martin Nielsen
Reply

Hi DW,

 

I have some code that saves product category field values on products, but when i hit the Save() method my code gives me this error:

 

Errors while running pipeline :
Message : Violation of PRIMARY KEY constraint 'EcomProducts$PrimaryKey'. Cannot insert duplicate key in object 'dbo.EcomProducts'. The statement has been terminated.
Source : .Net SqlClient Data Provider
Stack trace : at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at Dynamicweb.eCommerce.Products.Product.SaveAndConfirm(String ProductID, String ProductVariantID, String ProductLanguageID, Boolean skipExtendedSave) at Dynamicweb.eCommerce.Products.Product.Save()

When weird this is that i get the product from the database using the API, so i don't understand why this error is being thrown.

 

Here's a section of my code, and it's the Save() method that triggers the error.

 

 

 Dynamicweb.eCommerce.Products.Product product = Dynamicweb.eCommerce.Products.Product.GetProductByID(dbProduct.ProductID, dbProduct.ProductVariantID, dbProduct.ProductLanguageID);

      foreach (CategoryField categoryField in categoryFields)
      {
        string categoryFieldId = categoryField.SystemName;
        
        foreach (Integration.DAL.Objects.Field field in categoryField.Fields)
        {
          product.SetCategoryValue(categoryFieldId, field.SystemName, string.Join(",", field.Options));
        }
      }

      if (product != null)
        product.Save();

My product exists in 3 languages, and the works on danish languages, but gives the error above on GB and US languages.

 

Do you have any ideas to what could be wrong?

 

// Martin


Replies

 
Martin Nielsen
Reply

Okay, After posting the question, i found the solution, and i think it's a bug in the API.

 

In the API, it's not possible to see any other save methods that .Save(), even though there are a few overloads for the method.

 

When calling Save(), the API uses Context.LanguageID as the language when saving, and this it a bug when you've just used:

Dynamicweb.eCommerce.Products.Product.GetProductByID(dbProduct.ProductID, dbProduct.ProductVariantID, dbProduct.ProductLanguageID);

To retrieve your products. dbProduct.ProductLanguageID might not be the same as the context language, and therefore the correct choice would be to use "this.LanguageID".

 

Calling my save method like this fixed my error:

product.Save( product.ID, product.VariantID, product.LanguageID );

 

But it took me a few hours to find the issue :-(

 

// Martin

 

 
Lars Sørensen
Reply

I mentioned that to the guys last year. I think, we agreed that it would require a massive QA effort before releasing a change like that;-)

 
Martin Nielsen
Reply

Guess it's just a lesson learned then. I'll never make this mistake again :-)

 

Hiding the overloads for the Save method seems like the wrong move, with this bug around.

 

 

You must be logged in to post in the forum