Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the below code

public void addSeesion(int userid)
string qry = "insert into Session_Info(Session_Id,User_Id)values(@SessionId,@UserId)";
MySqlCommand cmd = new MySqlCommand(qry, Connection.Get());
cmd.Parameters.AddWithValue("@SessionId", session);
cmd.Parameters.AddWithValue("@UserId", userid);

There are 3 columns in the table including primary key. Now I want to get the last inserted record after this without making a new query. cmd.ExecuteScalar and cmd.LastInsertedId gives the primary key.

But I need the entire row or the Session_Id value. Is it really possible or there have any other way???

What I have tried:

cmd.ExecuteScalar and cmd.LastInsertedId gives the primary key.
But i want the entire record.
Updated 23-Oct-19 2:23am
ZurdoDev 19-Aug-16 7:58am    
What are you missing? You just inserted the record so you have all the values, right?
Member 12693361 22-Aug-16 0:07am    
Yes.but i have to make sure that the record is properly updated.
ZurdoDev 22-Aug-16 6:43am    
As long as you do not get an error and your sql is correct, then it worked. I'm not sure what you're looking for.
F-ES Sitecore 19-Aug-16 9:46am    
You'll need to do an additional query, there is no way around it. As Ryan said though, if none of the fields are computed in any way then you have all of the data anyway.
Member 12693361 22-Aug-16 0:06am    
of course i have the values.But How can i strongly believe that the record is updated with the exact values??

Try this...

Insert into [tableName]
    ([Col1], [Col2], [Coln])
  Output Inserted.*
Share this answer
CHill60 23-Oct-19 10:56am    
I've countered the downvote, but only with a 4 - the OP posted C# code and you have only included the SQL - for completeness I would have preferred to see an example of it being used in
Richard Deeming 23-Oct-19 11:11am    
I've counter-countered the vote - the OP was using MySQL, and as far as I can see that doesn't support the OUTPUT clause.
CHill60 23-Oct-19 11:17am    
Good one - I went on the tag and completely missed the object used in the code :-(
The way I would do it is simple: add a DATETIME column called InsertAt and set it's default value to GETDATE()
Then, all you have to do is:

Because you have given it a default, no other code should need changes.
Share this answer
F-ES Sitecore 19-Aug-16 9:46am    
If you are UserA and UserB causes another entry to be created between you inserting your record and doing the "select top" then you will get their data and not your own.
Member 12693361 22-Aug-16 0:09am    
I do not prefer another query.I just want the record as a return of command object.Am i asking something cannot possible??Please share your thoughts.

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