Solved

optimistic concurrency

Posted on 2009-04-14
4
689 Views
Last Modified: 2012-05-06
I doing a simple example to learn about optimistic concurrency.  

I have a winform app that displays the employees from the northwind database using a datagridview.  The form has a button click handler to update the datagridview.

In my class file, I have a method called RowUpdated that I want to call to check if there have been any changes to datagridview by another user.  How do I invoke the method so it will check any concurrency issues?

Do I need to add an event handler in the page load?

Thanks for any help.
Employee class file:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using OptimisticConcurrencyExample.NorthwindTableAdapters;

using System.Data;

using System.Data.SqlClient;

using System.Diagnostics;
 

namespace OptimisticConcurrencyExample

{

    class Employee

    {

        private EmployeesTableAdapter _adapter = null;
 

        public EmployeesTableAdapter adapter

        {

            get

            {

                if (_adapter == null)

                {

                    _adapter = new EmployeesTableAdapter();

                }

                return _adapter;

            }

        }
 

        public Northwind.EmployeesDataTable GetEmployees()

        {

            return adapter.GetEmployees();

        }
 

        public void UpdateDataSet(Northwind.EmployeesDataTable dt)

        {

            NorthwindTableAdapters.EmployeesTableAdapter eta = new EmployeesTableAdapter();

            eta.Update(dt);
 

            try

            {

                foreach (Northwind.EmployeesRow currentRow in dt)

                {

                    if (currentRow.HasErrors == true)

                    {

                        Debug.WriteLine(currentRow.EmployeeID + "\n" + currentRow.RowError.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                // Log errors here.

            }

        }
 

        public void RowUpdated(object sender, SqlRowUpdatedEventArgs e)

        {

            if (e.RecordsAffected == 0)

            {

                e.Row.RowError = "Another user may have recently modified this row.";

                e.Status = UpdateStatus.SkipCurrentRow;
 

            }

        }
 

    }

}
 

winform code-behind:
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;
 

namespace OptimisticConcurrencyExample

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();  

        }
 

        private void Form2_Load(object sender, EventArgs e)

        {

            Employee emp = new Employee();

            gvEmployees.DataSource = emp.GetEmployees();

        }
 

        private void gvEmployees_RowEnter(object sender, DataGridViewCellEventArgs e)

        {

            txtEmployeeID.Text = gvEmployees.Rows[e.RowIndex].Cells[0].Value.ToString();

            txtFirstName.Text = gvEmployees.Rows[e.RowIndex].Cells[2].Value.ToString();

            txtLastName.Text = gvEmployees.Rows[e.RowIndex].Cells[1].Value.ToString();

        }
 

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            Employee emp = new Employee();

            Northwind.EmployeesDataTable dt = new Northwind.EmployeesDataTable();

            dt = (Northwind.EmployeesDataTable)gvEmployees.DataSource;

            emp.UpdateDataSet(dt);

        }

    }

}

Open in new window

0
Comment
Question by:-Dman100-
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24144509
If you are using strong typed datasets, you can just check the "Use Optimistic Concurrency" checkbox of the configuration wizard.  There is also ways of doing it outside the DAL. Examples can be found : http://www.asp.net/Learn/data-access/tutorial-21-cs.aspx

Hope that helps.
0
 

Author Comment

by:-Dman100-
ID: 24148465
Hi wht1986,

Yes, I am using typed datasets.  I checked "Use Optimistic Concurrency" checkbox under the advanced section of the wizard, but when I call the update method with two forms open and make changes in the first form, and then try and update the second form, I get no message alerting me that changes might have already been made and that I'm overwriting changes?

What am I doing incorrectly?
0
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24149341
I have a simple form that has a datagridview bound to BindingSource that uses a typed dataset as its data source.

on form load, i fill the dataset with my table adapter.

I also have a button on my form, that when clicked issues an table adapter update on the dataset.

The typed dataset has optimistic concurncy on.

Irun 2 instances of the application, and edit the same record on both forms.  I click save one one form. and then I click the save on the other form. I get the messagebox saying the underlying data was changed.


        private void Form1_Load(object sender, EventArgs e)

        {

            this.usersTableAdapter.Fill(this.dataSet1.Users);

        }
 

        private void SaveButton_Click(object sender, EventArgs e)

        {

            try

            {

                this.usersTableAdapter.Update(this.dataSet1);

            }

            catch (System.Data.DBConcurrencyException exc)

            {

                MessageBox.Show("It appears another user has changed this record. Please refresh the data before editing.");

            }

        }

    }

Open in new window

0
 

Author Closing Comment

by:-Dman100-
ID: 31570258
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now