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

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

Editing one record

A form view can read a record and also change it. Switching to edit mode swaps the read only template for an editable one.

The update command

Markup
<asp:SqlDataSource id="oneProduct" runat="server"
    ConnectionString="<%$ ConnectionStrings:Shop %>"
    SelectCommand="SELECT Id, Name, Price FROM Product WHERE Id = @id"
    UpdateCommand="UPDATE Product SET Name = @name, Price = @price WHERE Id = @id">
    <UpdateParameters>
        <asp:ControlParameter Name="name" ControlID="nameBox" PropertyName="Text" />
        <asp:ControlParameter Name="price" ControlID="priceBox" PropertyName="Text" />
        <asp:QueryStringParameter Name="id" QueryStringField="id" />
    </UpdateParameters>
</asp:SqlDataSource>

What to watch

  • The update command needs the same identifier parameter as the select, or it will not match a row.
  • The edit template must contain controls with the identifiers the parameters expect.
  • Convert and validate values before the update runs.
Confirm the save. After updating, show a short confirmation so the visitor knows the change was stored.

Handling a file upload uses the same care with validation, in uploading a file and checking it.

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.