Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:46:06+00:00 2026-05-14T23:46:06+00:00

When creating a record grid with custom paging what is the best/optimal way to

  • 0

When creating a record “grid” with custom paging what is the best/optimal way to query the total number of records as well as the records start-end using C#?

SQL to return paged record set:

SELECT Some, Columns, Here FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY Column ASC) AS RowId, *
    FROM
        Records
    WHERE
        (...)
) AS tbl
WHERE ((RowId > @Offset) AND (RowId <= (@Offset + @PageSize)) )

SQL to count total number of records:

SELECT COUNT(*) FROM Records WHERE (...)

Right now, I make two trips to the server: one for getting the records, and the other for counting the total number of records.

What is/are the best way(s) to combine these queries to avoid multiple DB trips?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:46:07+00:00Added an answer on May 14, 2026 at 11:46 pm

    You can use stored procedure with output parameter (or return value) to pass total number of rows.

    create procedure dbo.Stuff_GetAll (
        @StartRowIndex int, -- zero based
        @MaximumRows int
    )
    as
    begin
        declare @TotalRows int
    
        select @TotalRows = count(*) 
        ...
    
        if (@TotalRows > 0 and @MaximumRows > 0)
        begin
            ;with T as (
                select *, row_number() over ()
                ...
            )
            select T.* from T
            where Row between @StartRowIndex + 1 and (@StartRowIndex + @MaximumRows)
        end
    
        return @TotalRows
    end
    GO
    

    You may want to add a check to query count() only when a first page is requested (can be quite expensive).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am dynamically creating sql query for inserting multiple records in one hit.Suppose no
I'm working on creating a SQL query that will pull records from a table
I'm creating a map grid (a 2D discrete number of points) using the concept
I need an example of creating t-sql query to load next 10 records (depends
I'm trying to implement a way of creating a record ( Update ) when
I am creating an exercise app that will record the weight used and the
I'm creating a class with extjs record in config. I keep a reference to
I'm trying to record sound by creating an android application. here is the code:
I am in the process of creating an input form on a record specific
I'm creating a cache which is going to contain records in Delphi 2007. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.