error trying to modify line items of PurchaseOrders

I load a purchaseOrder by using the GetPurchaseOrder() method, and I go into the lineitems, modify the property of one of the lineItems, and then run the Save() method. When I do this..it tells me I can't save because it's been modified by another user since it was loaded from the Database. I don't understand why i'm getting this error and i've looked into why it's caused and found that for some reason the LastModifiedAtLoad property is 1/1/0001 12:00 AM. Is there a reason my PurchaseOrder class isn't populating correctly

Thanks,

Dave




Answer this question

error trying to modify line items of PurchaseOrders

  • GoDaddy

    Dave,

    Can you provide a code snippet of how you retrieve and save the PurchaseOrder object along with the exception information

    Thanks,
    Colin



  • Ariel Mon

    Sure Colin,

    I have a custom "Request" class (which just represents a request for products) of which I have a property called "Order". Order is of type OrderGroup as it can be a Basket or a PurchaseOrder that I want to look at, depending if I'm looking at a past or current order. So, the get method looks kind of like this:

    //gets information from a local table containing info about all Requests (RequestID happens to be a custom GUID that I'm using to identify each request)

    // Orders is an aliased name and refers to CommerceContext.Current.Orders

    Request myRequest = GetRequestByID(requestID)

    if (request.RequestStatusCodeName == RequestStatus.WaitingForSubmission) //waiting for submission indicates to me it's a basket, so get the basket

    myRequest.Order = Orders.GetBasket(request.SoldToID, request.OrderGroupID);

    else //otherwise give me the Purchase Order that this represents

    myRequest.Order = Orders.GetPurchaseOrder(request.SoldToID, request.OrderGroupID);

    return myRequest;

    At this point, myRequest.Orders can be cast as a PurchaseOrder or a Basket since they both derive from OrderGroup. Everything works fine for Baskets, but for a purchaseOrder, if in my code I do something like:

    ((MyCustomLineItemClass)myRequest.Order.OrderForms[0].LineItems[0]).CustomStatusID = GetLineItemStatusIDByStatusCode(LineItemStatus.Cancelled);

    myRequest.Order.Save();

    I get an error from commerce server that says:

    PurchaseOrder could not be saved because another user has modified this Order after you loaded it from storage

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CommerceServer.OptimisticLockException: PurchaseOrder could not be saved because another user has modified this Order after you loaded it from storage

    Source Error:

    Line 360:    }
    Line 361:
    Line 362:    CurrentRequest.Order.Save();
    Line 363:
    Line 364:    _BindLineItemsList();

    Stack Trace:

    [OptimisticLockException: PurchaseOrder could not be saved because another user has modified this Order after you loaded it from storage]
      Microsoft.CommerceServer.Runtime.Orders.PurchaseOrder.Save(Boolean deleteOrder, ITransaction trans) +4679
      Microsoft.CommerceServer.Runtime.Orders.PurchaseOrder.Save(String applicationId, ITransaction trans) +679
      Microsoft.CommerceServer.Runtime.Orders.PurchaseOrder.Save() +9
      Request_ViewRequest.LineItemsList_RowDeleting(Object sender, GridViewDeleteEventArgs e) in c:\$\Projects\Research Store\Version 1\Code\Website\Request\ViewRequest.aspx.cs:362
      System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +133
      System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +604
      System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1155
      System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
      System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
      System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
      System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
      System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +115
      System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +171
      System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    

    Again, the above code works fine for Baskets, I'm not quite sure what the difference is here...

    What I'm doing is attempting to set the status of a line item to cancelled after it's been submitted. I have a grid that is bound to myRequest.Order.OrderForm[0].LineItems and when the user clicks the "Cancel" button, it runs the code I mention above, but it's not able to save it and bring back the grid with the new data, instead I get this error.

    Thanks for your help,

    Dave



  • error trying to modify line items of PurchaseOrders