Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WebService
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
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...........
[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..............
 
create PROCEDURE [dbo].[SP_InsertRow]
 
 @EMPName varchar(max),
 @DEsignation varchar(max)
AS
BEGIN
 SET NOCOUNT ON;
 
  insert into EMPDetails values(@EMPName,@DEsignation)
End
Posted 24-Aug-12 6:12am
Edited 24-Aug-12 11:44am
v3
Comments
Wes Aday at 24-Aug-12 11:19am
   
Why don't you just use a transaction in your stored procedure?
Christian Graus at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100