Skip to main content

Notifications

Business Central forum

A transaction must be started before changes can be made to the database.

Posted on by 124

Hello

I have created a C# console application by which I can able to sync data from CRM to Business Central (online). I am using web services to sync data from CRM to BC.

While trying to update the sales order line it was throwing an exception "A transaction must be started before changes can be made to the database.".

Initially, for the first time I can able to update the sales order line but after that, it continuously throws the exception. Even when read the sales order information through web services.

Any suggestions from the community much appreciated.

Categories:
  • A transaction must be started before changes can be made to the database.
    Hello,
    I have a similar situation, exposed standard page 42 "Sales Order" as a web service, and trying to import data from Postman into Business Central 22_CU3 OnPrem, throw SOAP request.
    I receive the same error
    "<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
        <s:Body>
            <s:Fault>
                <faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">a:Microsoft.Dynamics.Nav.Types.Exceptions.NavCSideException</faultcode>
                <faultstring xml:lang="en-US">A transaction must be started before changes can be made to the database.</faultstring>
                <detail>
                    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">A transaction must be started before changes can be made to the database.</string>
                </detail>
            </s:Fault>
        </s:Body>
    </s:Envelope>"
     
    To mention that no customizations are installed on the BC Instance, only Standard Microsoft app are installed.
    The line of Standard code where the error is throwed is in codeunit 60 "Sales-Calc. Discount", line 58 
     
    I have tested also on version BC23_CU2 and the result is the same error.
    The behavior is very strange, when making the request from Postman I received the error, although the record SalesHearder with the coresponding lines are created in BC.
    After inserting in BC, I cannot make a GET call from Postman for that particular record, it gives the same error "A transaction must be started before changes can be made to the database."
     
    When creating o record directly from Business Central, it doesn't brake on the same line....
    Is this a bug from MS, can anyone test this behavior?
     
    I am stuck on this!
    Thank you.
     
  • D365 Warrior Profile Picture
    D365 Warrior 124 on at
    RE: A transaction must be started before changes can be made to the database.

    Now the issue is happening even I am trying to read the sales order using this line

    var order = salesOrderService.Read("Sales order No");//read sales order no

    Even I can't able to read any sales order using sales order web service.

    Again thanks for your feedback.

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: A transaction must be started before changes can be made to the database.

    I can see some problems here. First, this line is not necessary if you don’t update the header:

    salesOrderService.Update(ref order);

    Then, when you read the Sales Order using the WS you should have also the lines in an array (no need to read them again).

  • D365 Warrior Profile Picture
    D365 Warrior 124 on at
    RE: A transaction must be started before changes can be made to the database.

    Hi Steffano

    Thank you very much for your great support.

    After dig down, I find out that my problem started from when 

    1. Read Sales order

    2. Only update the sales order line(no change in sales order)-- Sales order line

    Here is my code block

    using (var salesOrderService = GenerateBCProxyClass<Sales_Order_Service>(credential))
    {
    var order = salesOrderService.Read("Sales order No");//read sales order no

    var itemId = GetItemId(item, credential);// Read Nav ItemId

    var lines = order.SalesLines.ToList(); // Read all the existing sales lines from
    lines.Add(new Sales_Order_Line());
    order.SalesLines = lines.ToArray();
    salesOrderService.Update(ref order); //Update the sales lines to sales order

    using (var soLineService = GenerateBCProxyClass<salesDocumentLines_Service>(credential))// sales order line web service
    {
    var soFilter = new List<salesDocumentLines_Filter> { new salesDocumentLines_Filter { Field = salesDocumentLines_Fields.documentNumber, Criteria = order.No } };
    var soLines = soLineService.ReadMultiple(soFilter.ToArray(), "", 100);// read the existing sales order line
    if (soLines.Length > 0)
    {
    var lineToUpdate = soLines[soLines.Length - 1];
    lineToUpdate.quantity = quantity;
    lineToUpdate.quantitySpecified = true;
    lineToUpdate.number = itemId;
    lineToUpdate.unitPrice = unitPrice;
    lineToUpdate.unitPriceSpecified = true;
    soLineService.Update(ref lineToUpdate);//update the sales order line
    }
    }
    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: A transaction must be started before changes can be made to the database.

    This kind of error happens if you run report or page without calling COMMIT right before this action. Find the place where you manipulate with the data (insert or modify, probably delete). Consider calling COMMIT after this and before running report or page. I hope you understand that an incorrect call of COMMIT can cause serious proble with your data since changes won't be rollback if your page/report would fail. So be very attentive :)

    Good luck!

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: A transaction must be started before changes can be made to the database.

    Can you please post a piece of your C# code that you're using to create the Sales Order (header and lines)?

    This is a piece of C# code that works (full source on my GitHub page here github.com/.../NAV-AzureServiceBus):

    SalesOrder_Service ws = new SalesOrder_Service();
    ws.Url = YourURL;
    ws.UseDefaultCredentials = true;

    //Create the Sales Header
    SalesOrder order = new SalesOrder();
    ws.Create(ref order);

    //Here the Sales Order is created and we have the order no.

    //Update the Sales Header with details
    order.Sell_to_Customer_No = YourCustomerNo;
    order.Order_Date = YourOrderDate;

    int _rows = YourNumberOfRowsToCreate;

    if (_rows > 0)
    {
    //Create the Sales Lines array and initialize the lines
    order.SalesLines = new Sales_Order_Line[_rows];
    for (int i = 0; i < _rows; i++)
    {
    order.SalesLines[i] = new Sales_Order_Line();
    }
    }

    ws.Update(ref order);

    //Loads the data into the Lines
    if (_rows > 0)
    {
    int rowindex = 0;

    for (int rowindex = 0; rowindex < _rows; rowindex++)
    {
    Sales_Order_Line line = order.SalesLines[rowindex];
    line.Type = NAVSalesOrderWS.Type.Item;
    line.No = YourItemNo;
    line.Quantity = YourQuantity;
    rowindex++;
    }

    //Update the order lines with all the informations
    ws.Update(ref order);
    }

  • D365 Warrior Profile Picture
    D365 Warrior 124 on at
    RE: A transaction must be started before changes can be made to the database.

    No custom code (event subscribers) to the Sales header or Sales line table.

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: A transaction must be started before changes can be made to the database.

    Do you have some custom code (event subscribers) to the Sales Header or Sales Line table?

  • D365 Warrior Profile Picture
    D365 Warrior 124 on at
    RE: A transaction must be started before changes can be made to the database.

    Hi Stefano

    Thanks for your comments.

    I am using web services SOAP. Yes exposing the Sales Order page.

    1. Intially I am able to create Sales Order with sales order line using soap service and c# console app.

    2. While trying to update the sales line it was failed.

    3. Trying with different solution at last using the sales order line web service(soap) to update the sales order line.(only order line update, sales header as it is)

    4. I can able to update sales order line and it was updated successfully.

    5. I was creating a d365 crm plugin to sync crm to BC sales order sync. Registered it to the crm.

    6. After that, it throws exception "A transaction must be started before changes can be made to the database."

    7. Unregister the crm and again trying with the console application now this exception happens even while trying to read sales order via web service.

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: A transaction must be started before changes can be made to the database.

    Quite difficult without seeing the code. Are you using web services (SOAP) or REST APIs? If SOAP ws, are you exposing the Sales Order page?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans