0 votes
1 view
in SQL by (20.3k points)

I have this code:

string insertSql = 

    "INSERT INTO aspnet_GameProfiles(UserId,GameId) VALUES(@UserId, @GameId)";

using (SqlConnection myConnection = new SqlConnection(myConnectionString))



   SqlCommand myCommand = new SqlCommand(insertSql, myConnection);

   myCommand.Parameters.AddWithValue("@UserId", newUserId);

   myCommand.Parameters.AddWithValue("@GameId", newGameId);




When I insert into this table, I have an auto_increment int primary key column called GamesProfileId, how can I get the last inserted one after this so I can use that id to insert into another table?

1 Answer

0 votes
by (40.4k points)

In SQL Server 2005+:  If there is no insert trigger available, then try changing the insert statement (all in one line) to this

INSERT INTO aspnet_GameProfiles(UserId,GameId)


VALUES(@UserId, @GameId)

In SQL Server 2000, if there is an insert trigger like this:

INSERT INTO aspnet_GameProfiles(UserId,GameId) 

VALUES(@UserId, @GameId);


Then use this:

 Int32 newId = (Int32) myCommand.ExecuteScalar();

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 11, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !