September 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Develop Transactional .NET Web Services

  • July 26, 2004
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Implementation of ASP.NET Web Application

When you created the ASP.NET Web service client application, you saw how to initiate a transaction from the Web service by making use of the Transaction attribute. In the same way, you can create ASP.NET Web forms that can initiate a transaction and then include other objects in it. To accomplish this, you need to add an attribute to the Page Directive named transaction. By using this attribute, you can indicate to the ASP.NET runtime whether the page needs to be run in a transaction. The values you can use with the transaction attribute are Disabled, NotSupported, Supported, Required, and RequiresNew.

To illustrate the usage of the transactions in ASP.NET Web forms, consider the following code snippet:

<%@ Page transaction="Required" language="c#"
    Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
    Inherits="WebServiceTransactionsClient.WebForm1" %>

In the Page directive, you set the transaction attribute to Required, indicating that this ASP.NET page runs in a transaction. Next, you also need to add Web reference to the EmployeeService and DeptService. To do this, select Project->Add Web Reference from the menu and type in the location of the Web services. Then click the Add Reference command button to add those references.

In the Design view of the Web form, add the required controls for capturing dept and emp details, and then add a command button to save the entered information. In the Click event of the command button, add the following code:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
   try
   {
      int deptNo;
      DeptService dept    = new DeptService();
      EmployeeService emp = new EmployeeService();
      //Add the Dept details to the database
      deptNo = dept.AddDept(txtDeptName.Text,txtLocation.Text);
      //Add the Employee details to the database
      int empNo = 
         emp.AddEmp(txtEmployeeName.Text,txtAddress.Text,deptNo);
      lblResult.Text = "Transaction completed succesfully";
   }
   catch(Exception ex)
   {
      lblResult.Text = "Transaction failed";
   }
}

When you execute the above code, you get the screenshot in Figure 5 as a result.



Click here for a larger image.

Figure 5. Successfully Completed Transaction

Transactional Web Services Made Easy

You have seen how to create transactional Web services by using the classes supplied by the .NET Framework. You also saw how easy it is to leverage those transactional Web services from client applications such as an ASP.NET Web service and an ASP.NET Web application.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

Thiru Thangarathinam has six years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. He also possesses a thorough understanding of the software life cycle (design, development, and testing). He holds several certifications, including MCAD for .NET, MCSD, and MCP. Thiru is an expert with ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET Remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel