This is a migrated thread and some comments may be shown as answers.
Grid loading but Ajax is throwing 500 exception and I've found this through FireBug...
4 Answers 69 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Amit
Top achievements
Rank 1
Amit asked on 09 Nov 2010, 10:49 AM
As a normal developer I am using this control and the AJAX is throwing exception because for some unknown reason the GridAction is making a call to the database server with the order by content. The content type in database is of text.

I've tried it through [GridAction(Order = 3)] but no luck.

Controller has got this:
 
 [GridAction]
        public ActionResult AjaxPager(string sessionID, string ownerType, int ownerID, int? page)
        {
            var listData = GetNotes(ownerType, ownerID);
            return View(new GridModel(listData));
        }
 
private IEnumerable<NoteViewModel> GetNotes(string ownerType, int ownerID)
        {
            DataModel ds = new DateModel();
 
            return from o in ds.GetOwnerNotes(ownerType, ownerID)
                   select new NoteViewModel
                   {
                       NoteID = o.noteID,
                       Category = o.category,
                       Content = o.content,
                       Branch = o.branch,
                       DateMade = o.dateMade,
                       UserID = o.userID
                   };
        }


I don't know why it is doing a order by content.

exec sp_executesql N'SELECT TOP (10)
[Project1].[ownerID] AS [ownerID],
[Project1].[noteID] AS [noteID],
[Project1].[category] AS [category],
[Project1].[content] AS [content],
[Project1].[branch] AS [branch],
[Project1].[dateMade] AS [dateMade],
[Project1].[userID] AS [userID]
FROM ( SELECT
    [Extent1].[dateMade] AS [dateMade],
    [Extent1].[content] AS [content],
    [Extent1].[category] AS [category],
    [Extent1].[ownerID] AS [ownerID],
    [Extent1].[userID] AS [userID],
    [Extent1].[noteID] AS [noteID],
    [Extent1].[branch] AS [branch]
    FROM [dbo].[Note] AS [Extent1]
    WHERE ([Extent1].[ownerID] = @p__linq__0) AND ([Extent1].[ownerType] = @p__linq__1)
)  AS [Project1]
ORDER BY [Project1].[content] ASC',N'@p__linq__0 int,@p__linq__1 varchar(8000)',@p__linq__0=1,@p__linq__1='Employee'

Server Error in '/' Application.
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 09 Nov 2010, 11:12 AM
Hi Amit,

 The order by is emitted in two occasions:

  1. End user sorts a column of the grid
  2. Entity framework is used.
I suspect it is the second case which is creating the problem. Entity framework needs an OrderBy before calling Skip (which is required for paging). The grid executes the following code:
if (!sortDescriptors.Any() && queryable.Provider.IsEntityFrameworkProvider())
{
    // The Entity Framework provider demands OrderBy before calling Skip.
    SortDescriptor sortDescriptor = new SortDescriptor
    {
        Member = queryable.ElementType.FirstSortableProperty()
    };
    sortDescriptors.Add(sortDescriptor);
    temporarySortDescriptors.Add(sortDescriptor);
}
As you can see the member which is chose is returned by the FirstSortableProperty extension method. Here is its implementation:
internal static string FirstSortableProperty(this Type type)
{
    PropertyInfo firstSortableProperty = type.GetProperties().Where(property => property.PropertyType.IsPredefinedType()).FirstOrDefault();
 
    if (firstSortableProperty == null)
    {
        throw new NotSupportedException(TextResource.CannotFindPropertyToSortBy);
    }
 
    return firstSortableProperty.Name;
}

I guess this method returns the content property for some reason. You can try changing the position of the content property in the source file declaring the NoteViewModel object so it is not returned first.


All the best,
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
Amit
Top achievements
Rank 1
answered on 09 Nov 2010, 11:38 AM
Problem slept like a baby now!.

It has solved my problem but it can be a kind of great feature of your grid control if the column name can be selected . Of course paging need order by.

Now to solve this problem I'd to first create my own ViewModel, instead of the one to use for EF.

Thanks a ton!

 And another thing is that I cannot use "Filter" and "Sort" feature on Content (text type).
If you think the filter and sort problem can be fixed or work around would be great.

Just to add one more line on this that these problems are not happening on server based calls. Only Ajax are affected.
0
Amit
Top achievements
Rank 1
answered on 09 Nov 2010, 11:44 AM
While I am waiting for more reply from you. I have started drilling to change the column type from text to varchar(Max). I think I must use the feature of MS 2008 :)

Through this way I must be able to come out of any limitations such as filter etc...
0
Nick
Top achievements
Rank 1
answered on 10 Dec 2010, 04:22 PM
I think this approach is not entirely correct. If I expand the class EntityObject own properties that are not connected to the database, it is likely that the sorting will be selected by this attribute, which in turn results in an error LINQ to ENTITY. I propose to extend the functionality of the following methods:

in CustomAttributeProviderExtensions.cs add:
public static bool HasAttribute<T>(this ICustomAttributeProvider member) where T : Attribute
{
    return member.GetCustomAttributes(typeof(T), false)
                         .OfType<T>()
                         .Count() > 0;
}

in TypeExtensions.cs add:
internal static string FirstSortableProperty<T>(this Type type) where T : Attribute
{
    PropertyInfo firstSortableProperty = type.GetProperties().Where(property => property.PropertyType.IsPredefinedType() && property.HasAttribute<T>()).FirstOrDefault();
 
    if (firstSortableProperty == null)
    {
        throw new NotSupportedException(TextResource.CannotFindPropertyToSortBy);
    }
 
    return firstSortableProperty.Name;
}

in QueryableExtensions.cs change:
if (!sortDescriptors.Any() && queryable.Provider.IsEntityFrameworkProvider())
{
    // The Entity Framework provider demands OrderBy before calling Skip.
    SortDescriptor sortDescriptor = new SortDescriptor
    {
        Member = queryable.ElementType.FirstSortableProperty<System.Runtime.Serialization.DataMemberAttribute>()
    };
    sortDescriptors.Add(sortDescriptor);
    temporarySortDescriptors.Add(sortDescriptor);
}

This will be true, because all the properties associated with the data have the System.Runtime.Serialization.DataMemberAttribute
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Amit
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Share this question
or