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






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