Click here to Skip to main content
15,886,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

suppose,
I have following functions
C#
public void insertintoRegistration()
{
   //insert here
   //cmd.executenonquery()
}
public void insertintoCorr()
{
   //insert here
   //cmd2.executenonquery()
}

I want to have below lines in button click
C#
btninsert_click
{
   sqltransaction txn=con.begintransaction()
   cmd.transaction=txn;
   cmd2.transaction=txn;
   try
   {
      cmd.executenonquery();
      cmd2.executenonquery();
      txn.commit();
   }
   catch
   {
      txn.rollback();
   }
}

Now I want to have sqltransaction for these sqlcommands. How Can I do so, is is possible to do so or is there any else way to solve this problem.

Thanks
Posted
Updated 28-Sep-12 16:44pm
v3
Comments
ZurdoDev 28-Sep-12 16:24pm    
Just pass the transaction as a ref parameter.
Sandeep Mewara 28-Sep-12 17:05pm    
Your question is not quite clear. Please rephrase and update.
Gopal Rakhal 29-Sep-12 2:17am    
i have two functions for two separate insert command, now if i call both functions separately in button_click then one function may execute and another may not execute, so i want to execute both function if both are fine else want to roll back both insert commands..
fjdiewornncalwe 29-Sep-12 9:43am    
Did you check my answer below? It is exactly what you need.
[no name] 28-Sep-12 17:46pm    
Your question is not quite clear...Can u explain more...or are you getting any error

1 solution

As ryan has pointed out, you need to pass the transaction as a ref parameter.
Here is a good place to find out how: Performing Multiple Updates Within a Transaction[^]
 
Share this answer
 
Comments
Espen Harlinn 29-Sep-12 12:30pm    
5'ed!
fjdiewornncalwe 29-Sep-12 13:41pm    
Thanks Espen

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