Friday 28 June 2013

How To Handle Concurrency in LINQ to SQL using LinqDatasource in Asp.Net




What is Concurrency Control

Process of managing simultaneous execution of transactions in a shared database, to ensure the serializability of transactions, is known as concurrency control.

Why we need Concurrency Control

Simultaneous execution of transactions over a shared database can create several data integrity and consistency problems:

1.      Lost Updates.
2.      Uncommitted Data.
3.      Inconsistent retrievals
.
Concurrency Control Techniques

• Pessimistic concurrency control
• Optimistic concurrency control

Pessimistic Concurrency Control

         Pessimistic Concurrency Control assumes that conflicts will happen
         Pessimistic Concurrency Control techniques detect conflicts as soon as they occur and resolve them using blocking.

Optimistic Concurrency Control

         Optimistic Concurrency Control assumes that conflicts between transactions are rare.
         Does not require locking
         Transaction executed without restrictions
         Check for conflicts just before commit



 Description:-

                 Concurrency control theory has two classifications for the methods of instituting concurrency control:
 

Pessimistic concurrency control:-
                         in pressimistic concurrency control user can locked the data when user is readit.


Optimistic concurrency control:-
                       in pressimistic concurrency control user can locked the data when user is readit.



Insert,Update,Delete in Linq CRUD operation through Linq

Ajax Password Strength Example Check Password Strength

Display Gridview Row Detail in Javascript Popup Display Row Detail in Popup when mouse is over


Default.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Record Locking</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div>
        <asp:GridView ID="grdTerms" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataKeyNames="id" DataSourceID="EntityDataSource1" ForeColor="#333333" GridLines="None"
            OnRowDeleted="grdTerms_RowDeleted" OnRowUpdated="grdTerms_RowUpdated">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" InsertVisible="False" ReadOnly="True">
                    <HeaderStyle HorizontalAlign="Left" Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="fname" HeaderText="Name">
                    <HeaderStyle HorizontalAlign="Left" Width="175px" />
                </asp:BoundField>
                <asp:BoundField DataField="category" HeaderText="Category">
                    <HeaderStyle HorizontalAlign="Left" Width="100px" />
                </asp:BoundField>
                <asp:CommandField ButtonType="Button" ShowEditButton="True" />
                <asp:CommandField ButtonType="Button" CausesValidation="False" ShowDeleteButton="True" />
            </Columns>
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
        </asp:GridView>
 
        <asp:LinqDataSource ID="EntityDataSource1" runat="server" EnableDelete="True" EnableUpdate="True"
            ContextTypeName="dbLockDSDataContext" TableName="aby_temps">
        </asp:LinqDataSource>
        <br />
        To create new terms, enter the terms information and click Add New Terms<br />
        <asp:Label ID="lblError" runat="server" EnableViewState="False" ForeColor="Red">
        </asp:Label>
        <br />
        <br />
        <table>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                        Display="Dynamic" ErrorMessage="Description is a required field." ValidationGroup="Add"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Category:
                </td>
                <td>
                    <asp:TextBox ID="txtCategory" runat="server" Width="50px">
                    </asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCategory"
                        Display="Dynamic" ErrorMessage="Due days is a required field." ValidationGroup="Add">
                    </asp:RequiredFieldValidator>
                 
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="btnAdd" runat="server" Text="Add New Terms" ValidationGroup="Add"
            OnClick="btnAdd_Click" />
    </div>
    </form>
</body>
</html>





Default.aspx.cs:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

public partial class _Default : System.Web.UI.Page
{
    dbLockDSDataContext dbcontext = new dbLockDSDataContext();
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        aby_temp tblObj = new aby_temp();
        tblObj.category = txtCategory.Text;
        tblObj.fname = txtName.Text; ;
        try
        {
            dbcontext.aby_temps.InsertOnSubmit(tblObj);
            dbcontext.SubmitChanges();
            grdTerms.DataBind();
            txtName.Text = "";
            txtCategory.Text = "";
        }
        catch (Exception ex)
        {
            lblError.Text = "An error has occurred. " + ex.Message;
        }
    }
    protected void grdTerms_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            if (e.Exception.GetType() == typeof(ChangeConflictException))
            {
                lblError.Text = "Another user has updated or deleted " +
                    "those terms. Please try again.";
            }
            else
            {
                lblError.Text = "A database error occurred. " +
                    e.Exception.Message;
                e.KeepInEditMode = true;
            }
            e.ExceptionHandled = true;
        }
    }
    protected void grdTerms_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            if (e.Exception.GetType() == typeof(ChangeConflictException))
            {
                lblError.Text = "Another user has updated or deleted " +
                    "those terms. Please try again.";
            }
            else
            {
                lblError.Text = "A database error occurred. " +
                    e.Exception.Message;
            }
            e.ExceptionHandled = true;
        }
    }

}

0 comments:

Post a Comment