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

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

Inserting a row from a form

A data source control can write as well as read. Add an insert command and point its parameters at the form controls.

The insert command

Markup
<asp:SqlDataSource id="productsSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:Shop %>"
    InsertCommand="INSERT INTO Product (Name, Price) VALUES (@name, @price)">
    <InsertParameters>
        <asp:ControlParameter Name="name" ControlID="txtName" PropertyName="Text" />
        <asp:ControlParameter Name="price" ControlID="txtPrice" PropertyName="Text" />
    </InsertParameters>
</asp:SqlDataSource>

What to check

  • The parameter names must match the names used in the command exactly.
  • The form controls must exist on the page and have the properties you named.
  • The data types must match the columns, or the insert fails.
Validate before you write. Wrap the form controls in validation controls so an empty or malformed value never reaches the database.

Showing a single record rather than a list is the job of the form view.

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.