Tutorials ADO.NET Core Tutorial
Update Operations — Complete Guide
Update Operations — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.
On this page
ADO.NET Core Tutorial · Lesson 13 of 100
Update Operations
Foundations → SQL & safety → Production → Projects
Foundations · 1 — Connections & CRUD · ~6 min · Module 2: CRUD Operations
What is this?
UPDATE changes existing rows; check rows affected to detect missing ids.
Why should you care?
ShopNest cancels and status changes are updates — must be parameterized and often transactional.
See it live — copy this example
Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.
cmd.CommandText = @"
UPDATE Orders SET Status = @Status
WHERE Id = @Id AND Status = @FromStatus;";
cmd.Parameters.Add("@Status", SqlDbType.NVarChar, 20).Value = "Cancelled";
cmd.Parameters.Add("@FromStatus", SqlDbType.NVarChar, 20).Value = "Pending";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = orderId;
var n = await cmd.ExecuteNonQueryAsync(ct);
if (n == 0) throw new InvalidOperationException("not pending");
What happened?
- Optimistic guard with AND Status = @FromStatus.
- Zero rows means conflict.
Practice next
- Cancel a pending order.
- Retry when n==0.
- Update Total carefully with checks.
- Try canceling Shipped — expect 0.
- Set UpdatedAt = SYSUTCDATETIME().
Remember
Guarded updates. Check rows affected. Parameters always.
ShopNest cancel order
Only Pending → Cancelled allowed.
Outcome: No accidental cancel of shipped orders.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!