views:

492

answers:

1

Apologies up front, because this isn't a question but a solution - but it took a lot of searching to find out the answer and google wasn't much help, so I wanted to give something back to the community by providing the error and the solution to help future googlers.

When using LINQ to SQL, I ran into a problem when submitting changes (the second time) to a data table. The first time I submit changes all is OK, the second time I submit changes I recieved a SQL exception which said:

"The text, ntext, and image data types cannot be compared or sorted, 
 except when using IS NULL or LIKE operator"

The exception was thrown when I was calling SubmitChanges(), but because of the lazy loading of LINQ it actually arose in the previous line when I was calling Refresh(RefreshMode.KeepCurrentValues, myObject).

+2  A: 

The problem is that the Refresh mode is attempting to compare the current with the database version of all the fields, and SQL doesn't support that kind of comparison text, ntext and image columns (at least not on SQL Server 2000).

The solution is to add the following attribute to your column definition

UpdateCheck = UpdateCheck.Never

e.g.

[Column(DbType = "NText", CanBeNull = true, UpdateCheck = UpdateCheck.Never)]

Then the Refresh mode works OK and SubmitChanges doesn't have any further problems.

Alex White
or if you're on SQL Server 2005 and up, just leave those datatypes behind and never look back - use (N)VARCHAR(MAX) instead of (N)TEXT and VARBINARY(MAX) instead of IMAGE
marc_s
Ah, how I long to be able to upgrade everything to Sql Server 2005 (or even something more modern!). Unfortunately still stuck on Sql Server 2000 for the forseeable future!
Alex White
As you fixed your own problem, you should probably mark this as the answer.
Ryan Versaw
Good point, I've done so.
Alex White