Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am Using Begin trans in c# code

here is the case

begin trans

insert in table1

now here is the problem now i have to select the id inserted in above query

but i hvnt commit so the second qry wasnt executed

third qry is to insert in table2 using the id

commit tran

pls tell me solution
Posted
Comments
member60 5-Nov-11 6:22am    
what hvnt,qry ,pls means to u i don't know , but i think this is not the right way.
Rakesh S S 6-Nov-11 4:54am    
means to "u" here u means i guess you and not me lol..

Get the last Identity value after the Insert.

SQL SERVER – Retrieve Last Inserted Identity of Record[^]
 
Share this answer
 
Comments
RaisKazi 5-Nov-11 6:47am    
Yes "@@Identity" should help OP. 5ed.
One more resource - http://msdn.microsoft.com/en-us/library/ms187342.aspx
Not sure what you mean by using a Transaction in C#... What you show pretty much looks like SQL to me.
Perhaps you need to use a Transaction Object[^]?
It can be found in the System.Transaction Namespace[^].
Alternatively you can use TransactionScope[^]. You can now have multiple queries in a single Transaction without having to commit until you explicitly say so from your code. I guess it's the next best thing after SQL's own begin tran and end tran :)
Hope this helps!
 
Share this answer
 
Comments
RaisKazi 5-Nov-11 6:50am    
Valid points. 5ed.
Sander Rossel 5-Nov-11 6:55am    
Thanks Rais :)

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