Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

How to display last record column value in label using asp.net c#

am working on asp.net c# sqlserver 2005.

In my sqltable I have last record with DateColumn, Just I want to display this last record column value in label

This is my code.

C#
SqlDataReader rdr = SQLcmd.ExecuteReader();
    while (rdr.Read())
    {
lblCreatedDate.text =
    }
    string lastVal = rdr[0].ToString();
    rdr.Close()



Please help
Posted
Comments
DamithSL 2-Jun-14 3:57am    
this code will not compile
update question with the code which you actually tried

Your code will not compile. you can do as below
C#
SqlDataReader rdr = SQLcmd.ExecuteReader();
while (rdr.Read())
{
   //this will overwrite in every row but at the end you have the value of last row  
   lblCreatedDate.text = rdr[0].ToString();
}

since you only need one column value you better change the sql statement something like below
SQL
SELECT TOP 1 column_name FROM table_name
ORDER BY column_name DESC;

then you will get one row as the result.
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Jun-14 5:06am    
+5 for the commented line.
string col1Value = rdr["ColumnOneName"].ToString();
 
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