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 8706903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:43:57+00:00 2026-06-13T03:43:57+00:00

In Asp.net, I am trying to achieve pagination using following code: String query=SELECT *

  • 0

In Asp.net, I am trying to achieve pagination using following code:

String query="SELECT * "+
             "FROM (SELECT Distinct emp_name, emp_address, "+
             "         ROW_NUMBER() OVER(ORDER BY emp_id) AS rownum"+
             "       FROM Employee"+
             "     )as Person "+
             "WHERE rownum>"+ start +" and rownum <="+ end +";

SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

Above code does not retrieve Distinct rows.
How can I adjust my query to get distinct emp_name and order by emp_id along with total number of entries in single ExecuteReader()?

Currently I am calling ExecuteReader() twice first for data and and second for total count.

I followed SQL Server DISTINCT pagination with ROW_NUMBER() not distinct but was unable to understand how to implement it in my code. Please help.

  • 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-06-13T03:43:58+00:00Added an answer on June 13, 2026 at 3:43 am

    Use String.Format and raw string (@) to avoid concatenation.

    String query = String.Format(@"
        select emp_name, emp_address, rownum 
        from (
            select
                emp_name, emp_address,
                row_number() over(order by emp_id) as rownum
            from (
                select distinct emp_name, emp_address
                from employee
            ) s
        ) as Person 
        where rownum > {0} and rownum <= {1}
        ;
        select count(*) as total
        from (
            select distinct emp_name, emp_address
            from employee
            where rownum > {0} and rownum <= {1}
        ) s
        ;    
    ", start, end);
    
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();
    
    SqlDataReader reader = cmd.ExecuteReader();
    
    // gets the first data set
    while (reader.Read())
    {
        ...
    }
    
    // gets the second
    reader.NextResult();
    reader.Read();
    int total = (int)reader["total"];
    

    Use NextResult to read multiple result sets.

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

Sidebar

Related Questions

I am trying to achieve the following: I am using MySQL and ASP.NET. I
What I'm trying to achieve: Intercept requests for .asp files using an asp.net application
Using ASP.NET MVC when trying to get the information stored on my Session[objectName] from
I'm trying to write the following code inside ASP.Net-MVC razor view but the page
I'm new in asp.net and trying to pull values from Sqlserver into a listView
I am new to ASP.NET and am trying to convert a web application from
I'm using asp.net mvc2 and trying to send a list of json objects with
I am looking at ASP.NET MVC2 and trying to post a complex object using
I am using nhibernate and mvc3 in asp.net I'm trying to add data into
I'm working on an application using ASP.NET MVC 1.0 and I'm trying to inject

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.