Lesson 7
Modifying Data in Transact SQL Conclusion
Module 4 read VirtualBookShelf's data. This module changed it. Six lessons later, every piece of classic ADO's
Recordset object that once added, changed, or removed a row,
AddNew,
Update, and
Delete, has a working modern replacement, built with a single ADO.NET object,
SqlCommand[1], running against VirtualBookShelf's real
Customer and
Inventory tables in an ASP.NET Core Razor Pages application on .NET 10, talking to SQL Server 2025.
This page pulls those six lessons together: what each one built, how the legacy and modern pieces line up, and where this module's original objectives stand now that all six lessons are finished, including two lessons that ended up covering more ground than those original objectives named.
The Six Lessons, in Order
Lesson 1: Modifying Data Using Web Programming
Previewed
SqlCommand and
ExecuteNonQueryAsync as the pair that replaces
AddNew,
Update, and
Delete, sketched three handler methods,
OnPostUpdateAsync,
OnPostInsertAsync, and
OnPostDeleteAsync, without writing a complete version of any of them yet, and named this module's three objectives in the order the next three lessons would build them.
Lesson 2: Updating Database Records
Built the first working handler, an Update button against
Customer.Email, backed by a parameterized
SqlCommand and checked against how many rows it actually changed. Contrasted that against classic ADO's own hidden complexity, a Recordset update needed a keyset cursor and an optimistic lock type just to be legal in the first place, neither of which
SqlCommand has any equivalent for.
Lesson 3: Inserting Database Records
Built the second handler, collapsing the legacy Recordset's two-step
addRecord/
updateRecord sequence into one
INSERT statement, and introduced this lesson's one new technique, reading a generated
IDENTITY value back in the same round trip with
OUTPUT INSERTED.CustomerID and
ExecuteScalarAsync. Also built a narrowly scoped
NewCustomerInput bound model with no
CustomerID property at all, closing off an overposting attack by giving it nowhere to land rather than filtering it out afterward.
Lesson 4: Deleting Database Records
Built the third and simplest handler: a
DELETE statement needs no column list and no values, just a
WHERE clause. Closed with a side-by-side comparison of all three statement shapes built across these three lessons, and introduced soft deletes as a worth-knowing alternative VirtualBookShelf's current schema doesn't use yet.
Lesson 5: Data Types and Databases
Named the concern that had been sitting underneath every parameter in the three lessons before it: a value arriving from a request is always a string until something converts it. Replaced VBScript's single variant type and its
CInt/
CStr/
CDate family of conversion functions with ASP.NET Core's model binding and the
TryParse/
Convert methods that handle the same job wherever model binding doesn't reach.
Lesson 6: Direct Database Access
Named something this module had already been doing since Module 4: writing a
SqlCommand directly, with no Design Time Control or drag-and-drop Data Environment object standing in between. Traced the Data Environment's one genuinely useful idea, a named, reusable command, forward into a small private helper method, without reaching for a repository pattern or an ORM to get there.
From Recordset Methods to a Single SqlCommand Pattern
One thread runs through all six of these lessons: classic ADO gave a Recordset three separate methods for changing data, and ADO.NET replaces all three with one object, used three different ways. AddNew became an INSERT statement run through ExecuteScalarAsync when the new row's identity value needs reading back, or ExecuteNonQueryAsync when it doesn't. Update became an UPDATE statement run through ExecuteNonQueryAsync. Delete became a DELETE statement, run the same way. Every one of those three ADO.NET statements traveled through the exact same handler shape: open a connection, build a parameterized SqlCommand, run it, and check what it actually returned before deciding what to tell the visitor.
What changed alongside that renaming matters more than the renaming itself. A Recordset stayed connected to the database and tracked a current row in memory, which is why Update could be called after setting a few fields without naming that row again, and it's also why updating one required a specific cursor type and lock type just to be legal at all, a keyset cursor and optimistic locking, covered in full in Lesson 2. A SqlCommand has no concept of a current row and needs none, because every statement it runs names the row it affects directly in its own WHERE clause or VALUES list. That's not a feature lost in translation; it's a genuine simplification, one fewer kind of object to manage and one fewer way for a half-finished change to be left sitting in memory if something goes wrong partway through.
The Module's Three Objectives, and Two Lessons Beyond Them
Lesson 1 named three objectives before any of them had working code behind them: an Update button, an Insert button, and a Delete button. It's worth checking back against that list honestly, including the part of this module that went further than it did.
- Add an Update button that changes an existing row. Delivered in Lesson 2, against
Customer.Email, with the same pattern shown working against Inventory.QuantityOnHand as well.
- Add an Insert button that adds a new row. Delivered in Lesson 3, with a dedicated bound model, an
OUTPUT INSERTED clause for the new row's generated identity, and a specific defense against overposting built into the model's own shape rather than added on afterward.
- Add a Delete button that removes a row. Delivered in Lesson 4, the simplest of the three statements, with a confirmation step added on top even though neither ADO.NET nor Razor Pages requires one.
All three objectives landed exactly as Lesson 1 scoped them. Lessons 5 and 6, though, cover ground those three objectives never named at all. The legacy course placed a type-coercion lesson and a Data Environment lesson in this same module, and rather than skip them or force them into one of the three CRUD objectives where they didn't actually belong, this module kept them as what they are: a module-closing look at a concern, type coercion, that had been present in every one of the first four lessons without being named, and a reframing of a tool, direct script-based database access, that this module had already been practicing since the moment Lesson 2 wrote its first
SqlCommand. Lesson 6 said this directly rather than treating it as a new capability: the objective at the top of that lesson's page was satisfied before a single new line of it had been written.
Testing Before Committing: A Habit This Module Leaned On Harder
Module 4 established the habit of confirming a query in Azure Data Studio before moving it into a SqlCommand. This module didn't just continue that habit, it raised the stakes it protects against. A malformed SELECT run against the wrong table returns an empty or confusing result; a malformed UPDATE or DELETE, especially one missing its WHERE clause entirely, changes or removes rows that are gone the moment the statement commits. Lesson 4 named this directly: a DELETE deserves the testing habit more than any statement before it, since there's no undo once it commits, and the lesson's own two-statement check, running the DELETE and then a SELECT against the same row immediately afterward, confirmed both that the right row was targeted and that it was actually gone.
Lesson 3 extended the same habit to a statement with a return value worth confirming for a different reason: running an INSERT ... OUTPUT INSERTED.CustomerID statement directly in Azure Data Studio confirms not just that the statement is valid, but that it actually hands back the generated identity value a handler is about to depend on for its own redirect.
Parameterization, Still Non-Negotiable
Every SqlCommand built across all six lessons in this module parameterized every value a visitor could influence, with no exceptions. Lesson 2's UPDATE kept a new email address out of the SQL text with @NewEmail. Lesson 3's INSERT did the same for all four of a new customer's fields. Lesson 4's DELETE parameterized the CustomerID identifying which row to remove, explicitly calling out that a DELETE missing that protection is, if anything, more dangerous than an unparameterized SELECT or UPDATE, since a successful injection against it removes data outright rather than merely exposing or changing it.
Lesson 5 gave that discipline a name it had never had before in this course: coercion. Every one of those parameters depended on the value handed to it actually matching what its column expected, whether that match happened automatically through model binding or had to be checked by hand with TryParse for a value arriving outside it. Lesson 6 added one more piece worth remembering going forward: AddWithValue's own type inference, relied on in every parameterized command this module built, is convenient but not infallible, and an explicit SqlParameter with a declared SqlDbType is the fallback when that inference and a column's actual type don't line up.
How This Module Fits Between the Two Before It
This module didn't arrive out of nowhere, and it's worth placing it next to the two modules that came before it rather than treating it as a self-contained unit. Module 3 wrote UPDATE, INSERT, and DELETE as raw SQL, directly in Azure Data Studio, against VirtualBookShelf's real tables, with no application code wrapped around any of them at all. Module 4 built the application layer those statements would eventually live inside, SqlConnection, SqlCommand, SqlDataReader, Tag Helpers, and structured exception handling, but used all of it exclusively for reading data back out. This module is where those two threads actually meet: the exact statements Module 3 wrote by hand, running inside the exact application structure Module 4 built, changing data for the first time rather than only displaying it.
That sequencing explains why this module could move as quickly as it did. Lesson 2's Update handler didn't need to reintroduce what a SqlCommand is or how a connection string reaches a page model's constructor; Module 4 already covered both, thoroughly, and this module simply pointed back to them. What was actually new in each of this module's first four lessons was narrow and specific: which ADO.NET method runs a statement that doesn't return rows, how a generated identity value gets read back, and how a form's hidden fields carry the one piece of information, which row to act on, that a classic ADO Recordset once tracked silently in memory instead.
Frequently Asked Questions
Did this module ever actually use Entity Framework Core for any of its three operations? No, by design, and each lesson said so directly rather than leaving it unaddressed. Lesson 1 named EF Core's DbContext and SaveChangesAsync as a closer call than WinForms or Web Forms, since it's a real, current part of this course's future, but still not this module's tool: building the Update, Insert, and Delete statements by hand first is what makes EF Core's automatic version legible whenever this course reaches it.
Which lesson in this module would be hardest to skip? Lesson 2. Its Update handler establishes the exact shape, connect, build a parameterized command, run it, check the result, every later handler in this module reused without needing to re-explain. Lesson 3's Insert and Lesson 4's Delete both lean on that shape directly rather than inventing their own.
Why did Lesson 3's Insert handler need a new bound model, NewCustomerInput, when Lesson 2's Update handler used plain method parameters instead? Because the two handlers face different risks. An Insert handler creates a brand new row, so a bound model with a property for every column, including a sensitive one like an IDENTITY primary key, would be vulnerable to overposting, a malicious hidden field targeting a property the visible form never offered. A narrow model with no such property has nowhere for that field to land. An Update handler's explicit customerId and newEmail parameters reach the same safety a different way, there's no bound object at all for a hidden field to target.
Was anything in this module actually new to SQL Server 2025, rather than ordinary ADO.NET or C# syntax? Very little. Optimized Locking came up at FAQ level in a few lessons, consistently described only at the general level, a transaction-ID label plus a check against a row's latest committed value before locking it, never with the more specific lock-escalation claims some of this module's own research material reached for. Everything else, SqlCommand, ExecuteNonQueryAsync, ExecuteScalarAsync, model binding, TryParse, is ADO.NET and ASP.NET Core material that predates SQL Server 2025 by years.
Why did Lesson 5 spend an entire lesson on type coercion instead of covering it briefly inside Lesson 2? Because it's a concern that touches every single parameter this module builds, not one specific to updating a row. Folding it into Lesson 2 would have tied a general concept to one specific operation; giving it its own lesson, placed after all three CRUD handlers existed to point back at, let it name a pattern that had already shown up three separate times rather than introducing it before there was anything concrete to point to.
Does this module's error handling cover every way one of these three operations could fail? No, and it was never meant to. Each lesson covered the database-originated failures specific to its own statement, a missing row for Update and Delete, a duplicate email for Insert, plus the general SqlException catch-all Module 4 introduced. A validation failure, a visitor submitting an empty required field, is caught earlier, by ModelState.IsValid, before any of this module's ADO.NET code runs at all.
Could VirtualBookShelf's Customer and Inventory tables have changed structurally anywhere in this module? No. Every lesson read from or wrote to the exact schema Module 2 defined, unchanged. Lesson 4 specifically declined to invent a new foreign-key-bearing table just to make its error 547 example concrete, framing that handling as forward-looking instead, since no table in the schema as it stands today actually references Customer.
What's the one habit from this module worth carrying forward past it? Checking a statement's actual result rather than trusting that it ran. ExecuteNonQueryAsync succeeding and a statement actually matching the intended row are two different things, and every handler this module built, Update, Insert, and Delete alike, checked the returned row count or identity value before deciding what happened, rather than assuming success the moment no exception was thrown.
Did any lesson in this module reuse the same handler shape rather than inventing a new one each time? Deliberately, yes. Lesson 2's OnPostUpdateAsync set the pattern, open a connection, build a parameterized command, run it, check the result, and Lessons 3 and 4 followed that same shape for OnPostInsertAsync and OnPostDeleteAsync rather than restructuring it. Lesson 6 made the point explicit: once one handler is built correctly, the ones after it are mostly a different SQL statement and a different set of parameters wrapped around a shape that doesn't change.
What's Ahead
Before moving on, the Module 5 quiz below is worth taking, a check across the Update, Insert, and Delete handlers this module built, the type-coercion concern Lesson 5 named directly, and the direct database access Lesson 6 showed had already been built by that point.
Everything built across these six lessons carries forward unchanged: the connection string and page model patterns Module 4 established, the parameterization discipline every lesson in this module held to without exception, and the habit of testing a statement in Azure Data Studio before trusting it inside a handler. What's ahead builds on top of all three Update, Insert, and Delete handlers exactly as they stand, against the same two tables, with the same discipline, rather than replacing any part of it.
Modifying Data - Quiz
[1]SqlCommand: SqlCommand is a .NET (ADO.NET) class in System.Data.SqlClient or Microsoft.Data.SqlClient. It is used in C#/VB.NET code to execute T-SQL statements or stored procedures against a SQL Server database. SQL Server itself (T-SQL) has no command named SqlCommand. It uses standard commands such as SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, EXEC, etc. SqlCommand is the client-side object that *sends* those T-SQL commands to SQL Server.
