Andy Link Andy Link - 1 month ago 18
C# Question

Updating a record using Linq-to-SQL

I've done this many times and it has worked.

Every example I've found is exactly what I have..

I do my query...

var result = from u in tdc.tblUsers
where u.UserID == userID
select u;


and then I change the values I want to:

foreach (tblUsers u in result)
{
//change values (and no im not changing the primary key or foreign keys)
}


then I submit changes

tdc.SubmitChanges();


I have also tried it this way before and got the same error

tblUsers result = (from u in tdc.tblUsers
where u.UserID == userID
select u).Single();
result.Address = address;
result.Phone = phone;
tdc.SubmitChanges();


It will only ever retrieve 1 record with this query since
UserID
is the primary key.

When it hits submit changes, it throws exception that the row wasn't found or was changed. I am the only person using this so there's no other conflicts with accessing the db or locking. Why would it throw the
ChangeConflictException
? I have stepped through with debugger and the data persists all the way through the process, including the changes that I'm trying to make.

Answer

You are getting this error possibly because one of your fields has something different in the Linq To SQL designer and in the actual database.

Or you can take a look on this article

Good Luck!