Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a column in sql server with 15 records. In that 15 records I want for example 10 records to be displayed and later on 5 records should be displayed after button click like creating a bootstrap tab panel.

What I have tried:

Query:
select columnname from table group by columnname order by number asc

With this query in c# asp.net I am getting 15 records at a time.... How can I achieve this goal
Posted
Updated 28-Nov-18 0:05am
Comments
F-ES Sitecore 28-Nov-18 6:06am    
This is quite a large issue involving many things and there are many ways of doing it. Google for how to page through data using asp.net and whatever database access you are using (entity framework, ado.net, nhibernate etc) to get a feel for the problem space and what possible solutions there are. You haven't even said if you're using MVC, webforms etc, there simply isn't enough info in your question to give you specific help.

There are two parts to this: getting ten items is pretty easy, just use TOP:
SELECT TOP 10 columnname FROM table ORDER BY number asc
Note that you can't use the GROUP BY clause of your example, because if you do you can't use ORDER BY unless that column appears in the GROUP BY clause. This may help: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]

Getting the next ten is more complex:
SELECT columnname FROM 
 (SELECT ROW_NUMBER() OVER (ORDER BY columnname ASC) AS RN, columnname FROM table GROUP BY columnname) j
WHERE RN BETWEEN 11 AND 20
 
Share this answer
 
Comments
Member 8583441 28-Nov-18 6:39am    
this query works well sir "SELECT TOP 10 columnname FROM table ORDER BY number asc". sir can it be possible without using top
OriginalGriff 28-Nov-18 6:58am    
No, TOP is there to limit the number of rows. If you want generic pagin then you need to use the second form.
Member 8583441 28-Nov-18 7:13am    
then can you give me how to create it sir
OriginalGriff 28-Nov-18 7:34am    
Try it - the code is there!
Member 8583441 29-Nov-18 6:05am    
Thank you very much sir with this code "SELECT TOP 1 columnname FROM table ORDER BY number asc". Not with 10 but i changed it to 1 it worked like a charm.
The concept you are looking for is called "Paging".

Here are CodeProject articles that will help
ADO Recordset Paging in ASP[^]
ASP.NET Pagination[^]
How to implement Paging in ASP.NET at SQL Query Level[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900