Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As per your given code suppose I have webmethods for Delete and Update too,
And I need to consume them(WebMethods) in my web application where I also have to handle Transaction,
Can you provide with example of codes while consuming method, transaction should also gets handle.
Since I cannot use connection.BeginTransaction in my Consuming application becoz connection part is run inside webmethod

e.g.

/Application Consuming Webmethod
C#
try
{
//Here I need to handle Transaction if any error occurred in 
//middle of sth the transaction should get rolled back
service1.InsertRow(param1, param2);
service1.DeleteRow(param3, param4);
service1.UpdateRow(param5, param6);

}
Catch
{


}



web Service code...........
C#
[OperationContract]
public string InsertRow(string empname,string designation)
  { 
  SqlConnection dbConn = null;
  dbConn = new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["insightconnection"].ToString());
  
  SqlCommand cmd;
  SqlDataReader dr;
  
  dbcon.Open();
  spcmd = new SqlCommand("SP_InsertRow", spcon);
  spcmd.CommandType = CommandType.StoredProcedure;
  spcmd.Parameters.Add(new SqlParameter("@EMPName", empname));
  spcmd.Parameters.Add(new SqlParameter("@DEsignation", designation));

  dr = cmd.ExecuteReader();
  return "inserted......!!! ";
  }

Stored Procedure code..............

SQL
create PROCEDURE [dbo].[SP_InsertRow]
 
 @EMPName varchar(max),
 @DEsignation varchar(max)
AS
BEGIN
 SET NOCOUNT ON;

  insert into EMPDetails values(@EMPName,@DEsignation)
End
Posted
Updated 24-Aug-12 10:44am
v3
Comments
[no name] 24-Aug-12 11:19am    
Why don't you just use a transaction in your stored procedure?
Christian Graus 24-Aug-12 11:25am    
Also 'your given code' ? Is this an article you're talking about ? There's forum under the article for your questions.

1 solution

If I understood the question correcly, basically you have three options:

1.
Define a transaction inside the stored procedure. This would be feasible if the only thing you do is that you call this single procedure inside a logical unit of work.

2.
Utilize SqlTransaction[^] in your code. This lets you execute multiple, separate statements from client side in a single transaction

3.
Use System.Transactions[^], especially helpful if you need to embed local resources at client side into the transaction or if you need transaction propagation.
 
Share this answer
 

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