Related to ASP.NET MVC + LINQ exception
I get 2 exceptions at a random of 8-12 page reloads – which of the exceptions is also random, it could be number one, it could be number two. The exceptions are the following
[IndexOutOfRangeException: Index was outside the bounds of the array.]
System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i) +701
System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i) +29
Read_BoardMessage(ObjectMaterializer`1 ) +348
System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +42
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +472
System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
Website.BusinessLogic.Services.BoardMessageService.GetBoardMessageModelsFromQuery(IQueryable`1 boardMessagesQuery) in D:\Applications\Website.BusinessLogic\Services\BoardMessageService.cs:167
Website.BusinessLogic.Services.<>c__DisplayClass6.<GetStudentsBoardMessages>b__5() in D:\Applications\Website.BusinessLogic\Services\BoardMessageService.cs:89
Library.Web.Utilities.RequestScopeCache.Get(String key, Func`1 defaultGetter) in D:\APPLICATIONS\Library\Web\Utilities\RequestScopeCache.cs:66
Website.BusinessLogic.Services.BoardMessageService.GetStudentsBoardMessages(Int32 classId) in D:\Applications\Website.BusinessLogic\Services\BoardMessageService.cs:89
Website.Web.usercontrols.ShowMessages.Initialize() in D:\Applications\Website.Web\usercontrols\ShowMessages.ascx.cs:28
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064
—– and —–
[InvalidOperationException: Sequence contains more than one element]
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500
System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +41
System.Linq.Queryable.Count(IQueryable`1 source) +352
Website.Web.usercontrols.TopMenu.SetStudentMenu() in D:\Applications\Website.Web\usercontrols\TopMenu.ascx.cs:93
Website.Web.usercontrols.TopMenu.IsLoggedIn() in D:\Applications\Website.Web\usercontrols\TopMenu.ascx.cs:60
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064
The stack trace points to the following code
using (var dataContext = new MyDataContext(Config.ConnectionString))
{
int numberOfBoardMessages = dataContext.BoardMessages.
Where(boardMessage => boardMessage.BoardMessageClasses.
Any(boardMessageClass => boardMessageClass.ClassId == currentClassId) && boardMessage.IsPublished).
Count();
}
I have tried with a blank database with no ‘BoardMessages’ and the error still occurs. Does anyone have any ideas to what might be the problem?
Sounds like someone needs to use
TransactionScope.