Click here to Skip to main content
13,348,514 members (93,853 online)
Rate this:
Please Sign up or sign in to vote.
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


/Application Consuming Webmethod
//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);



web Service code...........
public string InsertRow(string empname,string designation)
  SqlConnection dbConn = null;
  dbConn = new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["insightconnection"].ToString());
  SqlCommand cmd;
  SqlDataReader dr;
  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)

  insert into EMPDetails values(@EMPName,@DEsignation)
Posted 24-Aug-12 6:12am
Updated 24-Aug-12 11:44am
Wes Aday 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

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

Solution 1

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

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.

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

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2018
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