Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

optimistic concurrency

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

Avatar of wht1986
wht1986
Flag of United States of America image

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.
Avatar of -Dman100-

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.