Implicit Transactions using TransactionScope : Transaction SqlConnection « ADO.net Database « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » ADO.net Database » Transaction SqlConnection 
Implicit Transactions using TransactionScope

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Transactions" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
    void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int categoryID;
            string connectionString = WebConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;            
            using (TransactionScope scope = new TransactionScope())
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {                    
                  categoryID = InsertCategory(connection);
                }
                scope.Complete();
            }
            lblResult.Text= "Category is written successfully. Category ID= " + categoryID.ToString();
        }
        catch (Exception ex)
        {
            lblResult.Text= "Exception is : " + ex.Message;
        }
    }
    
    int InsertCategory(SqlConnection connection)
    {
        string sql = "Insert into Production.ProductCategory(Name," +
                        "rowguid, ModifiedDate) Values(@Name, @rowguid, @ModifiedDate); SELECT @@IDENTITY";
        connection.Open()
        SqlCommand command = new SqlCommand(sql, connection);
        command.CommandType = CommandType.Text;
        SqlParameter nameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
        nameParam.Value = txtCategoryName.Text;
        command.Parameters.Add(nameParam);
        SqlParameter guidParam = new SqlParameter("@rowguid", SqlDbType.UniqueIdentifier);
        guidParam.Value = System.Guid.NewGuid();
        command.Parameters.Add(guidParam);
        SqlParameter modifieDateParam = new SqlParameter("@ModifiedDate", SqlDbType.DateTime);
        modifieDateParam.Value = System.DateTime.Now;
        command.Parameters.Add(modifieDateParam);
        int categoryID = Convert.ToInt32(command.ExecuteScalar());  
        return categoryID;      
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Implicit Transactions using TransactionScope</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblCategoryName" runat="server" Text="Category Name:" Width="179px"></asp:Label>        
        <asp:TextBox ID="txtCategoryName" runat="server"/>                
        &nbsp;<asp:Button ID="btnSave" runat="server" Text="Save" Width="92px" OnClick="btnSave_Click"/>        
        <br/><br/>
        <asp:Label ID="lblResult" runat="server" Font-Bold="true" Font-Size="Small" />
    </div>
    </form>
</body>
</html>

 
Related examples in the same category
1.Working with Transactions with an SQL Server Database
2.Open connection and begin transaction
3.Using Explicit Transactions using CommittableTransaction
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.