tags:

views:

89

answers:

2

In ASP.NET, what is the definitive way to pull one record from the database and bind it and HTML tag? Brevity and style points count.

A: 

For brevity, you'll want to use the SqlDataSource.

<asp:SqlDataSource ID="sql" runat="server" ConnectionString='<%$ ConnectionStrings:MyConnectionString %>'
    SelectCommandType="Text"
    SelectCommand="select MyField FROM MyTable WHERE ID = @id"
    >
    <SelectParameters>
        <asp:ControlParameter ControlID="txtUserName" PropertyName="Text" Name="id" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:BulletedList runat="server" DataTextField="MyField" DataSourceID="sql">
</asp:BulletedList>
Babak Naffas
How about in the code behind?
mmcglynn
A: 

It's not clear what you want but, detailsview control is designed for displaying only one record from data source.

DetailsView is a data-bound user interface control that renders a single record at a time from its associated data source, optionally providing paging buttons to navigate between records.

Canavar