Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends.

I want to fetch value of last identity column value generated from my c# application.
I am trying this code
SqlCommand cmd = new SqlCommand("SELECT IDENT_CURRENT('Orders')", cn);
            pid = (int)cmd.ExecuteScalar();

but it is not returning any value.

can any one help

Thanks and regards
Posted
Updated 18-Jan-12 0:58am
v2

RashdSiddique:
Use "Select MAX('coloumnname') from tablename"
This Might help to do the trick.
Happy Programming ;-)
 
Share this answer
 
Comments
RashdSiddique 18-Jan-12 6:49am    
Thanks for the solution.
But i have used your solution.Problem is if any of the row is deleted from the database then it will not give the last identity value inserted.
uspatel 18-Jan-12 7:01am    
This gives wrong results if multiple users inserts data at a time.
But it works for me

"SELECT IDENT_CURRENT('Orders')


Ok , Have you tried this SELECT MAX(ID) FROM Orders if id is the identitiy column
 
Share this answer
 
v2
Comments
RashdSiddique 18-Jan-12 6:50am    
Thanks for the solution.
But i have used your solution.Problem is if any of the row is deleted from the database then it will not give the last identity value inserted.
RDBurmon 18-Jan-12 7:07am    
No it will give you . Have you tried with the delete scenario ? Let say for example in Orders table there are id 1,2,3,4,5 then above query will return (5) and if row with id 3 got deleted then also above query will return (5)
RDBurmon 18-Jan-12 7:07am    
Plz check that you are using MAX() function not COUNT() function.
Use scope_identity() method in sql server
 
Share this answer
 
use the following statement :

SQL
DBCC CHECKIDENT (‘tablename’, NORESEED)


it works directly in sql server but not sure if it'll work from C#
 
Share this answer
 
 
Share this answer
 
Comments
RDBurmon 18-Jan-12 7:05am    
Uma Shankar , Rashd already specified that He is doing this in C# not in SQL SERVER
RashdSiddique 18-Jan-12 7:06am    
thanks friend.
But i want to retrieve identity of a particular table from my c# application.

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