Click here to Skip to main content
15,893,594 members
Articles / Programming Languages / C#

Optimistic Data Concurrency

Rate me:
Please Sign up or sign in to vote.
1.00/5 (12 votes)
5 Mar 20073 min read 25.8K   254   9  
Optimistic Data Concurrency
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;


public partial class _Default : System.Web.UI.Page 
{
    
      string strCheckupdated;
              string strupdated;

                    
     SqlConnection myConnection = new SqlConnection("server=10.10.10.57; uid=sa; password=12Test12; database=Northwind");

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        myConnection.Open();
        SqlCommand myCommand = new SqlCommand("Select UpdateAt from student where sno='" + TextBox3.Text + "'", myConnection);
                    
        byte[] b = new byte[100];
        b = (Byte[]) myCommand.ExecuteScalar();
        
        strCheckupdated = System.Text.ASCIIEncoding.ASCII.GetString(b);
            
                   
                 
            
            if (string.Compare(Session["tempString"].ToString(), strCheckupdated) == 0)
            {
                myCommand = new SqlCommand("Update student set name='" + TextBox1.Text+ "',address='" + TextBox2.Text+ "' where sno ='" + TextBox3.Text +"'", myConnection);

                try
                {
                    myCommand.ExecuteNonQuery();
                    Response.Write("<script> alert('Record Updated Successfully') </script>");
                }
                catch (Exception)
                {
                Response.Write("<script> alert('Error During Updation')</script>");
                }
                
                TextBox3.Text="";
                TextBox1.Text="";
                TextBox2.Text = "";
                                
            }

            else
            {
                Response.Write("<script> alert('Record Updated by another User')</script>");
                TextBox3.Text = "";
                TextBox1.Text = "";
                TextBox2.Text = "";
                
                            }
                            Session.RemoveAll();
                        myConnection.Close();
        }
    
           
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        string comstr = "SELECT name,address,UpdateAt FROM student where sno='"+ TextBox3.Text +"'";

        SqlCommand myCommand = new SqlCommand(comstr, myConnection);

        SqlDataReader reader = null;
        myConnection.Open();
        reader = myCommand.ExecuteReader();
        while (reader.Read())
        {
            TextBox1.Text = reader["name"].ToString();
            TextBox2.Text = reader["address"].ToString();

            byte[] b = new byte[100];
            b = (Byte[])reader["UpdateAt"];


            strupdated = System.Text.ASCIIEncoding.ASCII.GetString(b);
            
                
        }
        reader.Close();
        myConnection.Close();
  

        Session["tempString"] = strupdated;
        

    }
}

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


Written By
Software Developer
India India
Working as a .Net Developer.

Comments and Discussions