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.