Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.44/5 (4 votes)
See more:
I want to get the last inserted groupID int the table ProjectGroup

Here is the table

ProjectGroup
*GroupID
*GroupName
*ProjectName
Posted
Comments
DamithSL 13-Jun-14 9:13am    
what is the type of GroupID? is it auto number or auto increment?
Member 10239837 13-Jun-14 9:13am    
Auto Increment

here's the SQL:

SQL
SELECT TOP(1) GroupID FROM ProjectGroup ORDER BY 1 DESC


let me know if you need any code in C# using ADO or Linq
 
Share this answer
 
Comments
Member 10239837 13-Jun-14 9:18am    
Thank you very much it is working
Member 10239837 13-Jun-14 10:01am    
can you give me the code in c# please
goathik 13-Jun-14 12:35pm    
sure, just a sec
goathik 13-Jun-14 12:41pm    
SqlConnection connection = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand("SELECT TOP(1) GroupID FROM ProjectGroup ORDER BY 1 DESC",connection);

connection.Open();
SqlDataReader reader = command.ExecuteReader();

//won't need a while, since it will only retrieve one row
reader.Read();

//here is your data
string data = reader["GroupID"].ToString();

reader.Close();
connection.Close();
You have provided a list of columns, but no indication of the data type or usage.
Is the GroupId an identity field that automatically increases when a record is inserted?

If that is the case, then select the record with the maximum group id.

If, however, the group id can be inserted in any order, then you need to have a field that indicates the insertion order, such as insertion date. In this scenario, you can use the maximum insertion date.
 
Share this answer
 
Comments
goathik 13-Jun-14 9:17am    
nah, just try my solution ;)

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