views:

282

answers:

3

Ok, I'm using SQL Server Express 2008 and .Net 3.5 (c#)

I have a couple of datetime fields in the database and am trying to make an edit to a row (Using Linq-to-Sql) I receive the error "Row not found or changed."

I have spent some time getting the generated SQL and it seems that the issue is caused by the milliseconds attached to the datetime.

Generated SQL that does not work,

 @p5: Input DateTime (Size = 0; Prec = 0; Scale = 0) [20/10/2009 16:04:45]
 @p6: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 10:15:36]
 @p7: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 09:27:27]

AND ([SignUpDate] = @p5) 
AND ([LastActivityDate] = @p6) 
AND ([LastLoginDate] = @p7)

if i modify it myself like so it works,

 @p5: Input DateTime (Size = 0; Prec = 0; Scale = 0) [20/10/2009 16:04:45.390]
 @p6: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 10:15:36.733]
 @p7: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 09:27:27.747]

AND ([SignUpDate] = @p5) 
AND ([LastActivityDate] = @p6) 
AND ([LastLoginDate] = @p7)

What are my options in ways to get arround this?

Just to add this is my edit code,

var UserToEdit = this.GetUser(UserId);

UserToEdit.Forename = Fields["Forename"];
UserToEdit.Surname = Fields["Surname"];
UserToEdit.DateOfBirth = Convert.ToDateTime(Fields["DateOfBirth"]);
UserToEdit.DisplayName = Fields["DisplayName"];
UserToEdit.TelephoneNumber = Fields["TelephoneNumber"];

_db.SubmitChanges();
A: 
where foo.SignupDate >= signUpDate
  && foo.SignUpDate < signUpDate.AddSeconds(1)
  && foo.LastActivityDate >= lastActivityDate 
  && foo.LastActivityDate < lastActivityDate.AddSeconds(1)
  && foo.LastLoginDate >= lastActivityDate 
  && foo.LastLoginDate < lastActivityDate.AddSeconds(1)
KristoferA - Huagati.com
I'm not writing the SQL myself its generated by Linq to SQL
Pino
The above _is_ a linq to sql where clause.
KristoferA - Huagati.com
See the edit to my main post, as I said initially Im not generating the SQL im using the LINQ generated object
Pino
Ok, I thought the problem was with a select statement. If in the concurrency check where clause criteria for an update statement then turning off update checks for those columns (as in your answer) _or_ switching to use a timestamp for update concurrency checks is probably the best forward...
KristoferA - Huagati.com
Thanks :) - Pino
Pino
A: 

Since the only difference in your example is milliseconds then I would use SQL Profiler to determine if the original Select return milliseconds. Then see if you can fix that issue. It seems that the row data contains milliseconds but your select is not returning them.

After you do that and you still have the issue we can see what the next step will be.

Gary
Just to add, the actuall object that is returned from LINQ - The DateTime Object actually contains the Millisecond Value. But this value is not added to the LINQ Query?
Pino
I believe this my be a limitation of LINQ. Can you add a TIMESTAMP field to the table?
Gary
+2  A: 

See this link,

http://stackoverflow.com/questions/805968/system-data-linq-changeconflictexception-row-not-found-or-changed

# High precision datetime fields are used. The solution is to set

UpdateCheck to never for that column your DBML file

This has resolved my issue but feel a bit like a hack.

I'm leaving this open to see what others think.

Pino
You should not feel like it's a hack. Every user is identified by his/her ID value after all. Why should you care about LastActivityDate , which will change somewhat often, in the WHERE clause?
liggett78
Its basicly for optimistic locking. It allows us to check that the object hasnt changed since we got it.
Pino