Hey Guys,
I have a details view bounded with a sqldatasource.
I want to calculate the the total no of entries in details view and show it to the user. Please tell me how to do this??

Dani AI

Generated

A short, practical follow‑up to ’s question. ’s pager-based approach works well for showing “page X of Y” when paging is enabled and a PagerTemplate exists, but it depends on the pager being rendered and on what the select actually returns. Below are alternative ways to get a reliable total count and when to use each.

A single, explicit COUNT query (simple and accurate). Run the same WHERE/filter parameters as the DetailsView query and use ExecuteScalar to get the total:

int total;
using (var conn = new System.Data.SqlClient.SqlConnection(connString))
using (var cmd = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM Categories WHERE ...", conn))
{
    // add parameters matching the DetailsView filters
    conn.Open();
    total = (int)cmd.ExecuteScalar();
}
// assign total to a label or template field

Include the total in the same result set (no extra round trip). For SQL Server 2005+ the window function COUNT(*) OVER() returns the total as a column that can be bound in the DetailsView (e.g., Eval("TotalCount") in a TemplateField):

SELECT CategoryID, CategoryName, Description,
       COUNT(*) OVER() AS TotalCount
FROM Categories
WHERE ...
ORDER BY ...

Use the data source result directly. Casting the SqlDataSource.Select result to a DataView gives the row count returned by that query:

var dv = (System.Data.DataView)SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty);
int returnedRows = dv.Count;

Notes and pitfalls: ensure the COUNT uses the same filters/parameters as the DetailsView; avoid running expensive COUNT(*) on every postback (cache when appropriate); if paging or server-side LIMIT/TOP is applied, DataView.Count may reflect only the page; use the explicit COUNT or COUNT OVER when an absolute total is required.

Try this code

protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        // Get the pager row.
        DetailsViewRow pagerRow = DetailsView1 .BottomPagerRow;

        // Get the Label controls that display the current page information 
        // from the pager row.
        Label pageNum = (Label)pagerRow.Cells[0].FindControl("PageNumberLabel");
        Label totalNum = (Label)pagerRow.Cells[0].FindControl("TotalPagesLabel");

        if ((pageNum != null) && (totalNum != null))
        {
            // Update the Label controls with the current page values.
            int page = DetailsView1 .DataItemIndex + 1;
            int count = DetailsView1 .DataItemCount;

            pageNum.Text = page.ToString();
            totalNum.Text = count.ToString();
        }
}

Default.aspx

<form id="form1" runat="server">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
            DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" Height="50px" Style="z-index: 100;
            left: 195px; position: absolute; top: 225px" Width="125px" OnDataBound="DetailsView1_DataBound" OnPageIndexChanging="DetailsView1_PageIndexChanging">
            
            <Fields>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
                    ReadOnly="True" SortExpression="CategoryID" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            </Fields>

//Note This Part



            <PagerTemplate>
                Page <asp:Label id="PageNumberLabel" runat="server" /> 
                  of <asp:Label id="TotalPagesLabel" runat="server" />   
            </PagerTemplate>



        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
             SelectCommand="SELECT * FROM [Categories]" OnSelected="SqlDataSource1_Selected">
        </asp:SqlDataSource>
        
      
    
    </div>
    </form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.