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

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

Showing one record in a form view

A grid shows many rows. A form view shows one record at a time, laid out as an ordinary form.

The pairing

Markup
<asp:SqlDataSource id="oneProduct" runat="server"
    ConnectionString="<%$ ConnectionStrings:Shop %>"
    SelectCommand="SELECT Id, Name, Price FROM Product WHERE Id = @id">
    <SelectParameters>
        <asp:QueryStringParameter Name="id" QueryStringField="id" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:FormView id="productView" runat="server"
    DataSourceID="oneProduct" DefaultMode="ReadOnly">
    <ItemTemplate>
        <h2><%# Eval("Name") %></h2>
        <p>Price: <%# Eval("Price") %></p>
    </ItemTemplate>
</asp:FormView>

What to notice

  • The query string supplies the identifier that selects the record.
  • The item template decides how the record is laid out.
  • Eval reads a field from the bound row.
Handle the missing record. If the identifier matches nothing, the form view is simply empty. Show a friendly message rather than a blank panel.

To let the visitor change the record, continue to editing one record.

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.