Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Data » Downloads
 
Add your own
alternative version

ASP.NET Optimistic Concurrency Control

, 19 Aug 2003
How to implement the optimistic concurrency control without the DataSet
Concurrency_Control_src.zip
Concurrency
BLL
Concurrency.csproj.webinfo
Concurrency.vsdisco
Global.asax
Images
spacer.gif
Thumbs.db
SQLHelper
Stored Procedures
_vti_cnf
_vti_pvt
access.cnf
deptodoc.btr
doctodep.btr
service.cnf
service.lck
services.cnf
_vti_script
_vti_txt
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Concurrency
{
	/// <summary>
	/// Summary description for BasePage.
	/// </summary>
	public class BasePage : System.Web.UI.Page
	{
		private const string CONCURRENCY_STAMP = "ConcurrencyStamp";		// Keeps the Date time for the lastest object
		private const string CONCURRENCY_OBJECT = "ConcurrencyObject";		// Keeps a reference to the first object read

		private void Page_Load(object sender, System.EventArgs e)
		{
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Sets the original object
		/// </summary>
		/// <param name="obj">Object to store</param>
		protected void SetConcurrencyObject(BLL.CRUD obj)
		{
			ViewState[CONCURRENCY_OBJECT] = obj;
		}

		/// <summary>
		/// Gets the original object
		/// </summary>
		/// <returns>Object to retrieve</returns>
		protected BLL.CRUD GetConcurrencyObject()
		{
			return ((BLL.CRUD) ViewState[CONCURRENCY_OBJECT]);
		}

		/// <summary>
		/// Shows the fields that have concurrency conflicts
		/// </summary>
		/// <param name="controls">Mapped objects</param>
		protected void ShowConcurrencyFields(Hashtable controls)
		{
			// Retrieve the original object from the view state
			BLL.CRUD userObject = (BLL.CRUD) ViewState[CONCURRENCY_OBJECT];

			// Instantiate an object of the same type and read its properties
			Type type = userObject.GetType();
			BLL.CRUD dbObject = (BLL.CRUD) type.Assembly.CreateInstance(type.FullName);
			dbObject.ID = userObject.ID;
			if (!dbObject.Read())
				Response.Redirect("Error.aspx?msg=" + System.Web.HttpUtility.UrlEncode("The record has been deleted by another user."));

			// Get the product differences
			IList differences = BLL.ObjectDifference.GetDifferences(dbObject, userObject);

			// Reset any previous concurrency style from the page
			ClearPageConcurrency(controls);
			
			foreach (BLL.ObjectDifference diff in differences)
			{
				// Get the control
				WebControl ctrl = controls[diff.PropertyName] as WebControl;
				if (ctrl != null)
				{
					// Change the back color
					ctrl.BackColor = Color.LightGray;
					
					// Check for text boxes
					System.Web.UI.WebControls.TextBox txtBox = ctrl as System.Web.UI.WebControls.TextBox;
					if (txtBox != null)
					{
						txtBox.Text = diff.FirstValue.ToString();
						continue;
					}

					// Check for dropdown lists
					System.Web.UI.WebControls.DropDownList cmbBox = ctrl as System.Web.UI.WebControls.DropDownList;
					if (cmbBox != null)
					{
						cmbBox.Items.FindByText(diff.FirstValue.ToString());
						continue;
					}

					// Check for check boxes
					System.Web.UI.WebControls.CheckBox chkBox = ctrl as System.Web.UI.WebControls.CheckBox;
					if (chkBox != null)
					{
						chkBox.Checked = bool.Parse(diff.FirstValue.ToString());
						continue;
					}

					// ...... Extend to handle other controls .......
				}
			}

			// ViewState[CONCURRENCY_STAMP] = dbObject.Concurrency.ToOADate().ToString();
			ViewState[CONCURRENCY_OBJECT] = dbObject;
		}

		/// <summary>
		/// Resets the page's controls to normal state
		/// </summary>
		/// <param name="controls">Controls on the form</param>
		private void ClearPageConcurrency(Hashtable controls)
		{
			foreach (object obj in controls.Values)
			{
				// Get the control
				WebControl ctrl = obj as WebControl;
				if (ctrl != null)
					ctrl.BackColor = Color.White;
			}
		}
		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Teo
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 20 Aug 2003
Article Copyright 2003 by Teo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid