Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello,

I have a database named "sample", I displayed those sample database records in gridview. Its working.

What my task is, I have to show the last database record from sample database to all the rows of gridview.

For eg., a table named database_tbl present with 2 fields i.e., name and age.

I will display those records with gridview using sqldataadapter, ds() etc, suppose a record named "abcd" is the last database record in that table, I need that "abcd" to be displayed in all the rows of that particular gridview.

Thank You
Posted

My understanding is you want to show the name of the last record in a new column along with other records in your table. If this is the requirement check the below sample
SQL
DECLARE @database_tbl  TABLE
(
    ID INT IDENTITY(1,1),
    Name VARCHAR(100),
    Age INT
)

INSERT INTO @database_tbl
SELECT 'abc1', 12 UNION ALL
SELECT 'abc2', 13 UNION ALL
SELECT 'abc3', 18 UNION ALL
SELECT 'abcd', 21


DECLARE @LastRecordName VARCHAR(100)
SELECT TOP 1 @LastRecordName = Name FROM @database_tbl ORDER BY ID DESC

SELECT *, @LastRecordName AS LastRecordName FROM @database_tbl


Here we store the name of the last record in a variable and use that variable in the final query.
 
Share this answer
 
SQL
select [ID], [Name], [Age] from YourTable 
except 
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC
union all
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC

This line
SQL
select [ID], [Name], [Age] from YourTable 
except 
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC
will filter the last row and again add it using this line
SQL
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC
. The reason to exclude it and then including comes into play for the last page. If it is not done in this way, It will add the last record twice on the last page.
 
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