Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL: NullReferenceException in System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke)

Tags:

c#

linq-to-sql

The following bit of L2S code is giving me this very odd stack trace. If I attach a debugger and set a breakpoint on top of this debugger, it looks like the issue lies in evaluating transactions.Count(). Can anyone see specifically what would be causing this? Note that there are not rows in the Transactions table that fall in to this where clause.

The L2S Snippet:

var transactions = (from t in DataContext.Transactions
                    where t.TransactionDate.Date == date.Date && company.Id == t.Customer.CompanyId
                    select t);

if (transactions.Count() > 0) // Exception thrown here (transactions.Count())
{
    foreach (var transaction in transactions)
    {
        transaction.Detach();
    }
}

return transactions;

The Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke) +471
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1370
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +27
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +449
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +40
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +449
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +30
   System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate) +136
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +4173
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
   System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(Expression sequence, LambdaExpression lambda, SqlNodeType aggType, Type returnType) +84
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +6371
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +70
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1025
   System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +111
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +114
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +132
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +23
   System.Linq.Queryable.Count(IQueryable`1 source) +243
   MyProject.Repositories.TransactionRepository.GetTransactionsOn(DateTime date, Company company) in D:\Build\MyProject\Source\MyProject\Repositories\TransactionRepository.cs:38
   MyProject.Web.Widgets.RunningSalesTotalWidget.GetView() in D:\Build\MyProject\Source\MyProject\Web\Widgets\RunningSalesTotalWidget.cs:26
   MyProject.Web.WidgetController.Render(ViewContext viewContext) in D:\Build\MyProject\Source\MyProject\Web\WidgetController.cs:38
   MyProject.Web.Helpers.WidgetExtensions.RenderWidget(HtmlHelper helper, Int32 id) in D:\Build\MyProject\Source\MyProject.Web\Helpers\WidgetExtensions.cs:20
   ASP.views_home_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in d:\Build\MyProject\Source\MyProject.Web\Views\Home\Index.aspx:9
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Build\MyProject\Source\MyProject.Web\Views\Shared\Site.Master:30
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

Completely lost on this one. If anyone could provide any help here it would be greatly appreciated!

like image 657
Brad Heller Avatar asked Aug 06 '26 21:08

Brad Heller


1 Answers

One of your sub tables is probably the culprit. I just had this error and it was due to passing a null object into a helper function. Unfortunately you can't step through that deep.

like image 143
Joe Niland Avatar answered Aug 08 '26 11:08

Joe Niland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!