Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried this code to store max id in maxid var but it didnt get id in maxid

What I have tried:

int maxId;
SqlCommand dataCommand = new SqlCommand("Select max(Id) from Quant_Parent", con);
con.Open();
maxId = Convert.ToInt32(dataCommand.ExecuteScalar());
con.Close();
Posted
Updated 23-Aug-16 23:04pm
v2
Comments
Karthik_Mahalingam 24-Aug-16 5:01am    
does the table 'Quant_Parent' has primary key ID ?

1 solution

Don;t use MAX(ID): use @@IDENTITY instead - it returns the last IDENTITY value produced on a connection.
SQL
SELECT @@IDENTITY

Which means it returns the last value you created, rather than the guy at the next desk!
 
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