Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Removing a row

A delete removes a row. Because it is irreversible, it deserves more care than any other operation.

The command

C#
using (var conn = new SqlConnection(connString))
using (var cmd = new SqlCommand(
    "DELETE FROM Product WHERE Id = @id", conn))
{
    cmd.Parameters.AddWithValue("@id", productId);
    conn.Open();
    int removed = cmd.ExecuteNonQuery();
    statusLabel.Text = removed + " row removed";
}

Protect the visitor

  • Ask for confirmation before the delete runs.
  • Delete by identifier, never by a loose condition.
  • Consider a soft delete, where a row is marked as inactive instead of being removed, when the data matters.
A delete without a condition empties the table. Read your where clause twice before you run it, and test on a copy of the data first.

That completes the four data operations. To bind results without hand written loops, continue to data controls in practice.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.