Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
hi folks,

i want to display only latest data like 10 or 20....in gridview

if i click on more then it should display more records.

like facebook.
in facebook it loads only latest comments, but if you go in bottom then click more it will display more comments,
Posted
Comments
anushripatil 2-Jan-12 23:12pm    
u can select top 10 * from ur tablename order by desc .... & on click of more u can show ur remaining rows
Aniruddh Bhatt 2-Jan-12 23:39pm    
if i have 1000 data.
if i select top 10 and on click of more i dont want 990 data.i want another 10 means total 20. like facebook.
anushripatil 2-Jan-12 23:44pm    
ok , then on click of more if in the table u have id column u can do select top 10 * from urtablename where id>10 order by desc
Aniruddh Bhatt 2-Jan-12 23:49pm    
but what is next....
ok i got 20, but if user want another 10 then what?

hi Please check below query

SQL
Create PROCEDURE [dbo].[tblProductSelectByCatalogid] (
		@StrSortBy varchar(50)='Coumn name',
		@StrSortOrder varchar(50)='ASC/DESC',
		@intPageNum int=1,
		@intPageSize int=10,
)
AS

SET NOCOUNT ON

declare @SQL nvarchar(max)
Declare @Condition nvarchar(max)  
DECLARE @strSQLCount VARCHAR(MAX)
DECLARE @strFinalSQL VARCHAR(MAX)


SET @Condition = ''  



set @SQL ='
SELECT * FROM [tablename] WHERE Condition'

SET @SQL = @SQL + @Condition  



IF @intPageSize <> 0
	BEGIN
		SET @strFinalSQL = ' SELECT * FROM ('
		SET @strFinalSQL = @strFinalSQL + ' Select Row_Number() OVER (Order by ' + @StrSortBy + ' ' + @StrSortOrder + ') As RowNum,*  From( ' 
		SET @strFinalSQL = @strFinalSQL + @SQL
		SET @strFinalSQL = @strFinalSQL + ' )T )T1 ' 
		SET @strFinalSQL = @strFinalSQL + ' Where RowNum BETWEEN ' + Cast((@intPageNum - 1) * @intPageSize + 1 as varchar(5)) + ' AND ' + Cast(@intPageNum * @intPageSize as varchar(5)) 
	END
	ELSE
	BEGIN
			--SET @strFinalSQL = @SQLQuery
	SET @strFinalSQL = ' Select Row_Number() OVER (Order by ' + @StrSortBy + ' ' + @StrSortOrder + ') As RowNum,*  From( ' 
	SET @strFinalSQL = @strFinalSQL + @SQL
	SET @strFinalSQL = @strFinalSQL + ' )T'
	END

	EXEC (@strFinalSQL)
		print(@strFinalSQL)

	SET @strSQLCount = ' Select Count(*) as intTotalRecords  From( '
	SET @strSQLCount = @strSQLCount + @SQL
	SET @strSQLCount = @strSQLCount + ' )T'
	Exec (@strSQLCount)


in above query you can see that we pass the page no and page size
if we pass page no 1 and size equal to 10 then first it will take 10 record
next time we will increase page no to 2 then procedure will return 11-20 record like wise.

you can provide you combination by setting the Pagesize and page no.

Hope this will help :)
 
Share this answer
 
Comments
Aniruddh Bhatt 2-Jan-12 23:53pm    
it's work.
thanks
Nigam Patel 3-Jan-12 0:30am    
welcome
Please use Pagging concept in grid view for display data 10,20,30 or ....

To display latest record in Gridview for that you need to use primary key field and use ORDER BY with descending order.
 
Share this answer
 
Hi RealSoftBardoli
1)Either add a serial number column and make its identity true Or add date column in your database.
2)If you add Serial number then just get MAX(SERIAL_NUMBER) and if you add Date Column then use MAX(DATE)

You will get the result,for sure.
 
Share this answer
 
v2

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