LINQ To SQL Row not found or changed

LINQ To SQL likes to use TimeStamp columns for version tracking and makes it really easy to do so. However, in a lot of the apps I work on, we need to keep track of when the last change was made so we have a DateTime column titled LastUpdated. Using Steve Michelotti's post, I was able to use this column for row version control.

Everything was fine until I was using a DetailsView and got the message "Row not found or changed". This message is LINQ's way of telling you that the row has been changed and you now have a concurrence conflict. Since I was working on my local box, I knew this wasn't the case. Pulling up SQL Profiler, I was able to see that LINQ was sending the LastUpdated date back without milliseconds. Since this isn't the same as the value on the row, it thinks a change has occurred.

I was displaying the LastUpdated field in a readonly column so it shouldn't have been getting changed. I also had it as DataKey and could see that it was getting passed back. After farting around for a while, I finally figured out that it was the Label control in the EditItemTemplate that was causing the problem.

<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastUpdated") %>'></asp:Label>

The Bind in the above causes the DetailsView to use the value displayed in the label as the value for the update. Bind is two way even though the column is marked ReadOnly and ReadOnly really means "Don't let the user change anything." It doesn't mean "Don't let the value change."

Simply changing the Bind to Eval solves the problem as below.

<asp:Label ID="Label1" runat="server" Text='<%# Eval("LastUpdated") %>'></asp:Label>

I haven't tested but this should affect all of the other controls such as GridView and FormView.



Unknown said…
I don't see any harm in adding a separate Version column of type Timestamp, and then separate Created and Modified columns.
James said…
Oh man, this has been driving me MAD. Thank you for the post!

Popular posts from this blog

Simple Age Calculation

Windows 8 Keyboard Shortcuts

VS Removes Usings and Can't Find Types