I've been working on concurrency checking for an application I'm building, and a minor part of it has me slightly stumped.
I've got a DetailsView that populates from an ObjectDataSource, using business objects with optimistic concurrency checking built in in my Save() method (against a timestamp column). This can be proven to work perfectly in the following scenario:
- Start app, go to the DetailsView page (defaults to readonly), open a new window
- In one window, go to Update mode
- In the other window, go to Update mode, make a change, click Update
- In the first window, make a change and click update
This will cause my code to generate a DBConcurrencyException. All well and good. However, take the following scenario:
- Start app, go to DetailsView page, open a new window
- In one window, go to update mode, make a change, click update
- In the other window, go to Update mode
You will ender Update mode with the changed data. Now, technically, this works, however when this happens (data changed between the time it was opened for reading and the time it was opened for writing) I want to alert the user.
My first thought is to add something to the ModeChanged event for the DetailsView, but what to add to do the checking I am unsure about. Does anyone have any advice here?
Thanks!
Here's the relevant code.
EventDetails.aspx
1 <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EventDetails.aspx.cs" Inherits="EventDetails" Title="Untitled Page" %>
2 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
3 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Style="position: relative"
4 Width="125px" OnModeChanging="DetailsView1_ModeChanging" AutoGenerateRows="False" DataSourceID="ObjectDataSource1" DataKeyNames="ID,Timestamp">
5 <Fields>
6 <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
7 <asp:BoundField DataField="StartDateTime" HeaderText="StartDateTime" SortExpression="StartDateTime" />
8 <asp:BoundField DataField="EndDateTime" HeaderText="EndDateTime" SortExpression="EndDateTime" />
9 <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
10 <asp:CommandField ShowEditButton="True" />
11 </Fields>
12
13 </asp:DetailsView>
14 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="BSC.PID.BO.CalendarEvent"
15 SelectMethod="GetEvent" TypeName="BSC.PID.BLL.CalendarEventManager" UpdateMethod="Save">
16 <SelectParameters>
17 <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
18 </SelectParameters>
19 </asp:ObjectDataSource>
20 </asp:Content>
21
22
EventDetails.aspx.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 using BSC.PID.BO;
13 using BSC.PID.BLL;
14
15 public partial class EventDetails : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19
20 }
21 protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
22 {
23
24 }
25 }
26