In this article I will explain with an example, how to implement DetailsView control in ASP.Net using SqlDataSource. This article will also explain how to implement paging when DetailsView controls is populated using SqlDataSource.
I have made use of the following table Customers with the schema as follows.
Implement DetailsView with example in ASP.Net
I have already inserted few records in the table.
Implement DetailsView with example in ASP.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
Connection String
The following connection string has been defined in the Web.Config file. You will need to modify the connection string in order to connect to your database.
    <add name="constr" connectionString="Data Source=.\SQL2008R2;Initial Catalog=AjaxSamples;Integrated Security=true"/>
Implementing DetailsView using SqlDataSource in ASP.Net
The following HTML Markup consists of an ASP.Net DetailsView control with three BoundField columns and a SqlDataSource control.
This property is used to disable automatically display of records from database. Generally when BoundField or TemplateField columns are used, this property needs to be set to false as by default the value is true.
In order to enable paging in the DetailsView control, the AllowPaging property needs to be set to true. By default paging is disabled in DetailsView control.
The ID of the SqlDataSource control is set as DataSourceID for the DetailsView control. The ConnectionString property of SqlDataSource is set by specifying the name of the connection string setting in the Web.Config file.
The SqlDataSource control is specified with a SelectCommand which will execute Select Query over the Customers table and the fetched records will be displayed in the DetailsView control.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" AllowPaging="true"
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" HeaderStyle-CssClass="header" />
        <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-CssClass="header" />
        <asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-CssClass="header" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"
    SelectCommand="SELECT CustomerId, Name, Country FROM Customers"></asp:SqlDataSource>
Implement DetailsView with example in ASP.Net