Click here to Skip to main content
15,884,298 members

Need to Handle Transaction in web application, using webservice for DML operation

Member 4330673 asked:

Open original thread
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
Tags: Webservice

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900