views:

1099

answers:

3

I have RANDOM error (1 from 100 page loads) in following line of code:

topic = TopicsContext.GetCurrentDataContext().tTopics.Where(t => t.ContentId == contentId).SingleOrDefault();

Both ContentId property and conntentId local variables are long.

Most important - error occurs randomly, in most cases it works fine.

Thanks in advance for you ideas

Here is full Exception info:

 Error Message: Specified cast is not valid.
 Error Source: System.Data.Linq
 Error Stack Trace: 
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at Topics.BusinessLogic.Models.Services.TopicService.GetTopic(String title) in C:\rabota\topics\source\trunk\Topics.BusinessLogic\Models\Services\TopicService.cs:line 65
A: 

I think it happens from the "SingleOrDefault()" Method call. What it does, it will give you single answer if (t.contentId) exists (which should be an unique key, right?), but if the contentId is not exist in the database, then the Default value will be returned. The Default value by "default" will be null, that means the right hand side of the assignment (=) will be null and you are trying to cast null to a long.

For solution, you can set your topic as nullable type:

long? topic = ....

Or I will use "Single()" and wrap with try catch block to take care of the key not exists situation.

Anyway, it should not be any RANDOM error for the program.

xandy
No, topic is a tTopic, not long. The longs are ContentId and contentId
toast
I added try catch and will print all info when exception will happen next time.
Oh sorry I miss the point. Still, try to inject an non-existing contentId and see what will happen? just want to make sure if the singleordefault may give you the error.
xandy
A: 

I've gotten this when I generated the LINQ-to-SQL wrapper with one version of a DB and tried to use it against a different version of the DB. In my case, a column that was defined as short was changed to long, and a value came out of the DB that's not convertible to a long. Make sure your LINQ-to-SQL wrapper and database tables are in sync.

Jonathan
A: 

I got a similar error where I had a column of type varchar(1) which was mapped to char rather than string. This worked unless the column value was null when casting null to char threw the 'specified cast is not valid' exception.

See http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/ac91e587-6e91-454c-9fa2-bab20b7a258c for more details.

sgmoore