Developer forum

Forum » Development » Error when calling Save() method on Order

Error when calling Save() method on Order


Reply

I am adding custom products to the DW Basket and it works just fine. Except for this one situation:

 

When I add products to the basket and go to the basket and empty it and I try to add products to the basket again.

 

The first attempt fails with this exception:

 

 

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

   at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()

   at Dynamicweb.eCommerce.Orders.OrderLineCollection.get_Price()

   at Dynamicweb.eCommerce.Orders.Order.get_PriceBeforeFees()

   at Dynamicweb.eCommerce.Orders.Order.get_ShippingFee()

   at Dynamicweb.eCommerce.Orders.Order.Save(String IDStr)

   at Dynamicweb.eCommerce.Orders.Order.Save()

   at CustomModules.MyCart.BasketTester.AddProductToCart(String ProductNumber, Int32 amount)

 

the code that raises the exception is:

 

Order o = Dynamicweb.eCommerce.Common.Context.Cart;

if ( o == null )

{

  o = new Order();

  o.CustomerAccessUserID = dwUser.UserID;

  o.CustomerAccessUserUserName = dwUser.UserName;

  o.CustomerNumber = objUser.Number;

  o.Date = DateTime.Now;

  o.IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

  o.IsCart = true;

  o.StateID = OrderState.getDefaultOrderstate().get_Item( 0 ).ID;

  o.LanguageID = HttpContext.Current.Request["LANG"];

  o.ShopID = HttpContext.Current.Request["SHOP"];

  o.StepNum = 1;

  o.Save();

}

string productName =ProductNumber;

 

// Add Product As OrderLine

OrderLine ol = new OrderLine();

ol.Date = DateTime.Now;

ol.Order = o;

ol.OrderID = o.ID;

ol.ParentLineID = "";

ol.ProductID = ProductNumber;

ol.ProductName = productName;

ol.ProductNumber = ProductNumber;

ol.Quantity = amount;

ol.Type = "0";

ol.UnitPrice = new Dynamicweb.eCommerce.Prices.PriceInfo() { PriceWithoutVAT = 0, PriceWithVAT = 0 };

o.OrderLines.Add( ol );

o.Save();

 

and the error is at the line where I call o.Save() the second time (marked RED)

 

After I have got the exception I can add products to the order, so my thoughts is that the "Empty basket" thing is doing some nasty stuff to the Dynamicweb.eCommerce.Common.Context.Cart's OrderLines 

 

Does anyone have a workaround or fix for this issue?

 

 

 

 


Replies

 
Reply

Ever heard of try/catch? ;)

 

You need to save the orderline too. I'm sure it it will help in this instance, but could you please try and see if it makes any difference?

 
Reply
Sorensen wrote:

Ever heard of try/catch? ;)

 

You need to save the orderline too. I'm sure it it will help in this instance, but could you please try and see if it makes any difference?


 

try/catching is how I can give you the error :P

 

Well your suggestion gives me another SqlException:

 

Code:

 

Order

o = Dynamicweb.eCommerce.Common.Context.Cart;

if

{

o =

o.CustomerAccessUserID = dwUser.UserID;

o.CustomerAccessUserUserName = dwUser.UserName;

o.CustomerNumber = objUser.Number;

o.Date =

o.IP =

o.IsCart =

o.StateID =

o.LanguageID =

o.ShopID =

o.StepNum = 1;

o.Save();

}

( o == null )new Order();DateTime.Now;HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];true;OrderState.getDefaultOrderstate().get_Item( 0 ).ID;HttpContext.Current.Request["LANG"];HttpContext.Current.Request["SHOP"];

// Add Product As OrderLine

OrderLine

ol.Date =

ol.Order = o;

ol.OrderID = o.ID;

ol.ParentLineID =

ol.ProductID = ProductNumber;

ol.ProductName = productName;

ol.ProductNumber = ProductNumber;

ol.Quantity = amount;

ol.Type =

ol.UnitPrice =

ol.Save();

o.OrderLines.Add( ol );

o.Save();

ol = new OrderLine();DateTime.Now;"";"0";new Dynamicweb.eCommerce.Prices.PriceInfo() { PriceWithoutVAT = 0, PriceWithVAT = 0 };

 

Exception:


System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'OrderLineID', table 'dbo.EcomOrderLines'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
   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.Orders.OrderLine.Save(String IDstr)
   at Dynamicweb.eCommerce.Orders.OrderLine.Save()
   at CustomModules.BasketTester.AddProductToCart(String ProductNumber, Int32 amount)
 

 

 
Reply

I've attached a PageTemplateExtender I wrote last week for the same purpose. Could you try this?

 
Reply
Sorensen wrote:

I've attached a PageTemplateExtender I wrote last week for the same purpose. Could you try this?


 

That worked OK.

So I started to track which Property cause the error and it is failing when setting

Does that make sense?

ol.Type = "0";

 
Reply

OrderLineType refers to chosen entry from Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.

 
Reply
Sorensen wrote:

OrderLineType refers to chosen entry from Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.

The OrderLine.Type Property says something else "string":
 

public string Type { set; get; }
    Member of Dynamicweb.eCommerce.Orders.OrderLine

 

 

 
Reply

Yes, that's why we have things like this in our source code;)

 

If ol.Type = Base.ChkString(Base.ChkInteger(OrderLine.OrderLineType.Discount)) OrElse _
       ol.Type = Base.ChkString(Base.ChkInteger(OrderLine.OrderLineType.ProductDiscount)) 
    Then

 

The possible values of OrderLineType are:

 

Product = 0
Discount = 1
Fixed = 2
ProductDiscount = 3

 

 

You must be logged in to post in the forum