Updating Kentico Data as One Transaction

Editorial illustration.

Originally published 26 June 2023; rewritten and reviewed on 15 October 2026.

Anyone who has maintained a Kentico Xperience site for long enough has seen an awkward half-updated record. A custom form handler updates a UserInfo profile, then throws an exception right before saving the matching CustomTableItem or custom module Info object. Suddenly the member has been deducted points or marked as verified, while the audit record or order row never got written.

Fixing that is what database transactions are for, but wrapping an entire controller action in a transaction without thinking about the boundary creates new problems — especially lock contention and stalled SQL connections.

Define the exact boundary that must succeed together

A transaction should cover only the database writes that belong to a single atomic business operation. In Kentico Xperience 12 and 13 (and legacy versions using CMS.DataEngine), CMSTransactionScope is the built-in wrapper that manages the underlying SQL transaction across Kentico’s InfoProvider calls.

Suppose a member redeems a voucher: you need to update the voucher status and insert a redemption log together. Neither should exist without the other.

using System;
using CMS.DataEngine;

public void RedeemVoucher(int voucherId, int userId)
{
    using (var scope = new CMSTransactionScope())
    {
        var voucher = VoucherInfo.Provider.Get(voucherId);
        if (voucher == null || voucher.IsRedeemed)
        {
            throw new InvalidOperationException("Voucher is unavailable.");
        }

        voucher.IsRedeemed = true;
        voucher.RedeemedByUserID = userId;
        voucher.RedeemedWhen = DateTime.UtcNow;
        VoucherInfo.Provider.Set(voucher);

        var log = new RedemptionLogInfo
        {
            VoucherID = voucherId,
            UserID = userId,
            CreatedWhen = DateTime.UtcNow
        };
        RedemptionLogInfo.Provider.Set(log);

        scope.Commit();
    }
}

Two details matter here. First, CMSTransactionScope implements IDisposable and rolls back automatically when disposed if scope.Commit() was never reached. Older blog snippets sometimes invent a scope.Rollback() method that does not exist on CMSTransactionScope — let the using block handle disposal on exception instead. Second, keep CMSTransactionScope as short as possible so SQL Server does not hold row or page locks longer than necessary. You can verify the exact Commit and constructor behaviour in the Xperience 13 API reference.

Keep exceptions visible and external calls outside

A classic mistake is putting a try/catch inside the using (var scope = new CMSTransactionScope()) block, logging the error, and swallowing it before scope.Commit(). If you catch and continue to Commit(), you have just committed a partial write. Either let the exception bubble out of the using block so disposal rolls the transaction back, or rethrow it cleanly after logging at the caller boundary.

Just as important: never put an SMTP email send (EmailEngine.SendEmail), a payment gateway callback, or an external HttpClient call inside CMSTransactionScope. A database rollback cannot unsend an email or reverse an external webhook. Worse, if the remote API takes eight seconds to time out, your SQL transaction holds locks on Kentico tables for all eight seconds, which under moderate traffic quickly snowballs into SQL timeout errors across the whole site.

Run the external side effect after scope.Commit() succeeds. If guaranteed delivery matters — for example, sending a receipt or syncing to an ERP — write an outbox row inside the same CMSTransactionScope and let a Kentico scheduled task process the outbox asynchronously.

Verify partial failure before shipping

Do not assume a transaction works just because the happy path saves both rows. In your local or staging environment, temporarily throw a new Exception("Simulated failure") right between the first Provider.Set() and the second Provider.Set(). Trigger the action, confirm the exception surfaces cleanly to the caller, and inspect both tables directly in SQL Server Management Studio (SSMS) to make sure the first write was genuinely rolled back.

Also check how the code behaves if a user double-clicks the submit button. CMSTransactionScope protects atomicity, not idempotency — you still need a status check, a unique constraint, or a guard against concurrent requests trying to redeem the same record twice.