This is a migrated thread and some comments may be shown as answers.

The argument to DbIsNullExpression must refer to a primitive or reference type

24 Answers 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sL342799
Top achievements
Rank 1
sL342799 asked on 09 Dec 2009, 09:34 PM
Hi,

I am trying to implement a grid with ajax binding (backend is using LINQ and Entity Framework). Originally, I was getting the infamous Server Error '500' due to circular references. So I decided to follow the advice on the forums and implement a ViewModel approach (Implementing ViewModel).

I created a simple ViewModel as:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
 
namespace UndertakingTracker.Models 
   
    public class UndertakingViewModel 
    { 
 
        public int undertakingID {  
             
            get;  
            set
         
        } 
 
    public string undertakingReferenceNumber {  
         
        get;  
        set
     
    } 
     
    public DateTime? dateReceived {  
         
        get
        set
    } 
     
    public string portfolioName {  
         
        get;  
        set
     
    } 
     
    public string propertyName {  
        get;  
        set;  
     
    } 
     
    public string lenderName {  
         
        get;  
         
        set;  
     
    } 
    public string status {  
         
        get;  
        set
    } 
 
         
    } 


In my repository I did the following:

public IEnumerable<UndertakingViewModel> listAllUndertakingsAsViewModel() {  
         
        return from u in _entities.Undertaking.Include("Lender").Include("Lender.Address").Include("Property").Include("Property.Address"
            select new UndertakingViewModel{ 
             
            undertakingID = u.undertakingID, 
            undertakingReferenceNumber = u.undertakingReferenceNumber, 
            dateReceived = u.dateReceived, 
            portfolioName = u.Property.portfolioName, 
            propertyName = u.Property.propertyName, 
            lenderName = u.Lender.lenderName, 
            status = u.status 
      
            }; 
          
        } 

In my controller I did the following (where service simply calls the repository and passes back the IEnumerable):

 public ActionResult Index() 
        { 
 
            return View(_service.listAllUndertakingsAsViewModel()); 
        } 
 
        [GridAction] 
        public ActionResult _AjaxBinding() 
        { 
 
             
             return View(new GridModel<UndertakingViewModel> 
        { 
            Data = _service.listAllUndertakingsAsViewModel() 
        }); 

And Finally my view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UndertakingTracker.Models.UndertakingViewModel>>" %> 
 
[snip] 
 
<% 
        Html.Telerik().Grid(Model) 
            .Name("Undertakings"
            .Ajax(ajax => ajax.Action("_AjaxBinding""Undertaking")) 
            .Columns(columns => 
            { 
                columns.Add(u => u.undertakingReferenceNumber).Title("UTI").Filterable(true); 
                columns.Add(u => u.dateReceived).Title("Date Received").Format("{0:dd/MM/yyyy}"); 
                columns.Add(u => u.portfolioName).Title("Portfolio"); 
                columns.Add(u => u.propertyName).Title("Property"); 
                columns.Add(u => u.lenderName).Title("Lender"); 
                columns.Add(u => u.status).Title("Status"); 
                columns.Add(u => u.undertakingID).Format(Html.ActionLink("Edit""Edit"new { id = "{0}" })).Encoded(false).Title("Edit").Filterable(false).Sortable(false); 
                columns.Add(u => u.undertakingID).Format(Html.ActionLink("Details""Details"new { id = "{0}" })).Encoded(false).Title("Details").Filterable(false).Sortable(false); 
                columns.Add(u => u.undertakingID).Format(Url.Action("Create""UndertakingActivity"new { id = "{0}" })).Encoded(false).Title("&nbsp;").Filterable(false).Sortable(false); 
 
            }) 
             
            .Pageable() 
            .Sortable() 
            .Filterable() 
            .Render(); 
%> 

Upon rendering, i get the following exception and stacktrace:
System.ArgumentException was unhandled by user code 
  Message="The argument to DbIsNullExpression must refer to a primitive or reference type." 
  Source="System.Data.Entity" 
  StackTrace: 
       at System.Data.Common.CommandTrees.DbIsNullExpression..ctor(DbCommandTree cmdTree, DbExpression arg, Boolean isRowTypeArgumentAllowed) 
       at System.Data.Objects.ELinq.ExpressionConverter.CreateIsNullExpression(DbExpression operand, Type operandClrType) 
       at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter parent, Expression input) 
       at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.NotTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.NotEqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.ConditionalTranslator.TypedTranslate(ExpressionConverter parent, ConditionalExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) 
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
       at System.Data.Objects.ELinq.ExpressionConverter.Convert() 
       at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) 
       at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
       at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() 
       at Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each[T](IEnumerable`1 instance, Action`1 action) 
       at Telerik.Web.Mvc.UI.Grid`1.WriteRows(IGridRenderer`1 renderer) 
       at Telerik.Web.Mvc.UI.Grid`1.WriteTable(IGridRenderer`1 renderer) 
       at Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer) 
       at Telerik.Web.Mvc.UI.ViewComponentBase.Render() 
       at Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.Render() 
       at ASP.views_undertaking_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\UndertakingTracker\UndertakingTracker\Views\Undertaking\Index.aspx:line 16 
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
       at System.Web.UI.Control.Render(HtmlTextWriter writer) 
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
       at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\UndertakingTracker\UndertakingTracker\Views\Shared\Site.Master:line 48 
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
       at System.Web.UI.Control.Render(HtmlTextWriter writer) 
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
       at System.Web.UI.Page.Render(HtmlTextWriter writer) 
       at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) 
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
  InnerException:  
 

Any ideas on how to resolve?

Thanks!


24 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 10 Dec 2009, 11:17 AM
Hello sL342799,

 Unfortunately I am not sure why this exception is thrown. The only thing which comes to mind is to ask you if the ViewModel class contains the DB key of the actual model entity. If not please add it to the ViewModel class. If it already does I have to ask you to send a runnable sample showing the problem. Please send it to atanas.korchev at telerik.com


Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
IQworks
Top achievements
Rank 1
answered on 30 Jan 2010, 02:25 AM
  HI Atanas,
     Was a running sample ever submitted for this Entity Framework issue ?  Forgive me, I am already speaking to this in another post, but I thought I would ask here as well.
 
  thanks for your time.    
    
0
Atanas Korchev
Telerik team
answered on 01 Feb 2010, 08:27 AM
Hi IQworks,

No, we haven't received a running sample demonstrating this problem.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Roy Wang
Top achievements
Rank 1
answered on 25 Feb 2010, 06:24 AM
Hi, sL342799

Did you solve the problem? I have the same problem. Thanks!

regards,
Roy
0
Atanas Korchev
Telerik team
answered on 25 Feb 2010, 07:17 AM
Hi,

To all guys getting this exception - please try upgrading to the latest version 2009.3.1320. Let me know if the problem persists.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Raman
Top achievements
Rank 1
answered on 29 Mar 2010, 05:01 PM
Hi Guys,

Were you able to reproduce and debug this problem? I'm facing similar problem as mentioned by "sL342799".
But in my case, I'm using Server binding and it throws this exception only when I try to filter any column.

Thanks,
WebExpert
0
IQworks
Top achievements
Rank 1
answered on 29 Mar 2010, 08:41 PM

Hi webXpert Xpert ,

     I have a note I made to myself here that might help .....

     

When i first got this error, it was because I needed to use the telerik version that had the fix - 2009.3.1320. This went for scripts and content as well. Telerik.web.mvc.dll for this version has to be in the references.

If its not, you have to add it as a reference and do a "Clean" and "Rebuild". When you do the clean it should remove the dll from your bin folder as well - yep, it goes to your bin and deletes old files. at that point, you may have to add it manually to the bin, and make sure its referenced and then you do a build.

NOW THIS WAS A JUMBLED SEQUENCE OF EVENTS, BUT MAKING SURE THE DLL VERSION WAS REFERENCED, CLEANED AND REBUILT (not neccessarily in that order) fixed it.

I know its not much to answer why, I should of noted it, but hope this is helpful. ?

0
Makoto
Top achievements
Rank 1
answered on 16 Apr 2010, 04:54 PM
I too am having this issue.

I only get this issue when I filter a column, I don't have a problem getting data, sorting or paging.

Seems to be an issue with Entity Framework.  I'm guessing it's the way that the Grid control is add expressions to my IQueryable<> object.

I don't want to convert this to a memory list (.ToList()) since then I'll lose the small SQL data sets, and will end up grabbing the whole dataset before the Grid gets a chance to build up the expressions on the SQL side.

I might have to crack open the original Telerik.Web.Mvc code and see where it uses DbIsNullExpression.

0
amir ehsani
Top achievements
Rank 1
answered on 28 Apr 2010, 12:43 PM
I have exactly the same problem as Makoto, and im using Telerik.web.mvc.dll 2010.1.309, ajax binding and viewmodel. viewmodel contains primary key of entity.
0
IQworks
Top achievements
Rank 1
answered on 28 Apr 2010, 04:50 PM

Hi Amir and Makoto,

     If you have not already seen this post, it might give further clues.

      http://www.telerik.com/community/forums/wpf/gridview/dbisnullexpression.aspx 
best wishes

0
amir ehsani
Top achievements
Rank 1
answered on 01 May 2010, 06:28 AM
well, I prefer to keep LINQ query as long as possible and do not use ToList until its really necessary. Currently I'm using ToList to solve the problem, but im looking for alternative solution to add filters to LINQ query, if there is any.
0
Alan Wong
Top achievements
Rank 1
answered on 21 May 2010, 02:22 PM
I also am getting this error, using the exact same pattern, but only when I try to apply a filter. The error is generated from within EF. Something with the Telerik grid filter is causing this... is it trying to apply a null filter on a non-nullable column perhaps?
0
Atanas Korchev
Telerik team
answered on 21 May 2010, 02:52 PM
Hi Jeff Jorczak,

Is there a chance to send us a sample project? We have not been able to reproduce this exception.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Alan Wong
Top achievements
Rank 1
answered on 21 May 2010, 04:32 PM
I was able to create a very basic project to reproduce the error. I has been sent to your attention. Thank you for the customer support!
0
Mike
Top achievements
Rank 1
answered on 18 Aug 2010, 03:10 PM
Greetings,

Would just like to add that I'm experiencing the same issue. Ajax binding, EF, using version 2010.1.309.235.

.ToList() does "fix" the issue, but that's not really a good work-around - kind of defeats the purpose.

Regards,
Mike

Edit: Looking at the stack trace and stepping through the source, it no doubt is happening in the .Count method of QueryableExtensions when it tries to call .Execute on the provider. Unfortunately, I don't know how to fix :(
0
Mike
Top achievements
Rank 1
answered on 17 Sep 2010, 02:08 PM
Thanks to Telerik support, turns out it's no longer an issue with the latest drop - 2010.2.825.

Thanks Telerik! I appreciate you opening a support ticket even for your free version.

Mike
0
Tom
Top achievements
Rank 1
answered on 03 Feb 2012, 10:17 AM
I still get the error in version 2011.3.1306.340 when I'm using custom binding to filter a query that includes a projection.
0
Atanas Korchev
Telerik team
answered on 03 Feb 2012, 10:25 AM
Hi,

How are you performing the custom filtering? Are you using custom code or something provided by Telerik?

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Tom
Top achievements
Rank 1
answered on 03 Feb 2012, 10:43 AM
Updated:  I should add that this is using Entity Framework 4.2.0.0

Hello Atanas.  That was a fast response.

I'm using the custom binding on the server side.  The filtering is almost verbatim from what's listed on the telerik demo site for grid.  The only difference being I'm using a different Model.

public static IQueryable<ProductItemModel> ApplyFiltering(this IQueryable<ProductItemModel> data,
    IList<IFilterDescriptor> filterDescriptors)
{
    if (filterDescriptors.Any())
    {
        data = data.Where(ExpressionBuilder.Expression<ProductItemModel>(filterDescriptors));
    }
    return data;
}

The ApplyFiltering method doesnt get called until after I have a query that includes some joins and a projection in to a viewModel. When the filter is applied the exception occurs when getting the Count.

//Apply filtering
data = data.ApplyFiltering(command.FilterDescriptors);
ViewData["Total"] = data.Count(); // Exception here
0
Atanas Korchev
Telerik team
answered on 03 Feb 2012, 11:02 AM
Hi,

 This could occur if your are having a "select" before the "filter". Entity Framework may get confused in this case. You can try performing the "select" (projection from the db type to the view model) after all other operations (paging, sorting, filtering etc).

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Tom
Top achievements
Rank 1
answered on 03 Feb 2012, 11:12 AM
That's what I was afraid of.  Since some of the fields in the projected viewmodel include columns from joined tables I'm not sure how well applying the filter before the select will work out.  I'll give it a go anyway.  I'm not really needing this functionality critically.  I can create my own filter controls to work around the issue.

Interesting, If I manually add a filter instead of using the ExpressionBuilder with the GridCommand Entity Framework has no problem with it.
//Apply filtering
data = data.ApplyFiltering(command.FilterDescriptors); // No FilterDescriptors used in this test
data = data.Where(c => c.ProductDesc.Contains("Phone"));
ViewData["Total"] = data.Count(); // This works just fine even with the Where clause up above
0
Tom
Top achievements
Rank 1
answered on 03 Feb 2012, 11:37 AM
Just as a followup for this thread, applying the filters before the select indeed didn't work in this situations because of the joined tables. 

Given a select statement similar to this:
var data = product from data
   select new ProductItemModel()
   {
        ID = product.ID,
      ProductDesc = product.productDescriptor.ProductDesc
   };

ProductDesc wouldnt be able to be filtered.
0
Atanas Korchev
Telerik team
answered on 06 Feb 2012, 09:33 AM
Hello,

Cant you do something like this?

var data = product from data
   where product.productDescriptor.ProductDesc.Contains("Phone")
   select new ProductItemModel()
   {
        ID = product.ID,
      ProductDesc = product.productDescriptor.ProductDesc
   };

Regards,

Atanas Korchev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tom
Top achievements
Rank 1
answered on 06 Feb 2012, 10:23 AM
I kind of ended up doing something like that.  The trick is to change the FilterDescriptors in the GridCommand object that's passed in to the Grid Select Action.

The algorithm I used looked something like this:

  1. Get IQueryable without Select
  2. Traverse the GridCommand FilterDescriptors (recursively to handle CompositeFilterDescriptors) and convert filterDescriptor.Member to the non-projected name ( change ProductDesc to product.productDescriptor.ProductDesc in our contrived example)
  3. ApplyFiltering
  4. ApplySorting (use the non-projected expression, product.productDescriptor.productDesc, in AddSortExpression method)
  5. ApplyPaging
  6. Add the select (projection)
  7. ApplyGrouping (use the projected expression, ProductDesc, in ApplyGrouping method)

Those steps got me to where I wanted to be.  Thanks for your help.

Tags
Grid
Asked by
sL342799
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
IQworks
Top achievements
Rank 1
Roy Wang
Top achievements
Rank 1
Raman
Top achievements
Rank 1
Makoto
Top achievements
Rank 1
amir ehsani
Top achievements
Rank 1
Alan Wong
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Tom
Top achievements
Rank 1
Share this question
or