DataOperation - System.InvalidCastException: 'Invalid cast from 'System.String' to 'System.Guid'.'

Hi, I'm using the React Grid and consuming the data with the UrlAdaptor from my ASP.NET Core 3.1 backend. I'm able to fetch the data from the server and to display the records in the grid, as well as to sort and group them. However, when I'm filtering the data through the grid search box, I'm always getting the following errror:

System.InvalidCastException
  HResult=0x80004002
  Message=Invalid cast from 'System.String' to 'System.Guid'.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Syncfusion.EJ2.Base.QueryableOperation.PredicateBuilder[T](IQueryable`1 dataSource, List`1 whereFilter, String condition, ParameterExpression paramExpression, Type type)
   at Syncfusion.EJ2.Base.QueryableOperation.PredicateBuilder[T](IQueryable`1 dataSource, List`1 whereFilter, String condition, ParameterExpression paramExpression, Type type)
   at Syncfusion.EJ2.Base.QueryableOperation.PerformFiltering[T](IQueryable`1 dataSource, List`1 whereFilter, String condition)
   at Syncfusion.EJ2.Base.DataOperations.PerformFiltering[T](IQueryable`1 dataSource, List`1 whereFilter, String condition)

My Url Adaptor controller looks like the following:

[HttpPost]
        [Route("[action]")]
        public ActionResult<Models.Task> UrlAdaptor([FromBody] DataManagerRequest dm)
        {
            var data = _context.Tasks.AsQueryable();
            DataOperations operation = new DataOperations();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                data = operation.PerformSearching(data, dm.Search);  //Search
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                data = operation.PerformSorting(data, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                data = operation.PerformFiltering(data, dm.Where, dm.Where[0].Operator); -----> ERROR
            }
            int count = data.Count();
            if (dm.Skip != 0)
            {
                data = operation.PerformSkip(data, dm.Skip);   //Paging
            }
            if (dm.Take != 0)
            {
                data = operation.PerformTake(data, dm.Take);
            }
            return dm.RequiresCounts ? Ok(new { result = data, count = count }) : Ok(data);
        }


A single entry of the data collection of the grid looks like the following:

{"ID": "4f903fb5-ebb2-4b23-a2a1-428428a9cb03","Description": "Buy a new PC monitor","Done": false,"Priority": false,"PrivateOfID": "d5f1914c-0660-4583-99ec-4ff9bf6a001f","PrivateOf": null,"Private": true,"Progress": 0.2,"TaskCategoryID": "6a1e9cda-989d-444f-b131-3cad41b5bb49","TaskCategory": null,"DueDate": "2021-01-01T00:00:00","ContactID": null,"Contact": null,"RequestedByID": null,"RequestedBy": null,"ResponsibleForID": null,"ResponsibleFor": null,"CreatedAt": "2020-10-14T10:17:29.303705","CreatedByID": "02673469-9abc-4c4e-ad44-517bf2d45e23","CreatedBy": null,"CreatedByName": "Max Mustermann","UpdatedAt": "2020-10-14T10:17:29.303705","UpdatedByID": "02673469-9abc-4c4e-ad44-517bf2d45e23","UpdatedBy": null,"UpdatedByName": "Max Mustermann"},

1. Any idea how can I solve this problem?
2. By the way, is there somewhere a complete example on how I can use an ASP.NET Core 3.1 backend im combination with the Rest API Adaptor with a React Grid (instead of using the UrlAdaptor). In particular, I'm looking for an elegant way how to solve the filtering in combination with Linq (Entity framework)?

3 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team October 20, 2020 06:21 AM UTC

Hi Laurin, 

Greetings from Syncfusion support. 
 
We can reproduce the reported behavior at our end and we have confirmed this is an issue from our side and logged a bug for the same as Filtering Guid is not working when the dataSource as IQueryable type”. At Syncfusion, we are committed to fixing all validated defects (subject to technical feasibility and Product Development Life Cycle ) and will include the defect fix in our upcoming patch release Nov 04th, 2020. 
 
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.  


Regards, 
Rajapandiyan S


WI williamholding September 14, 2021 05:00 AM UTC

An InvalidCastException is thrown when cast from one type to another type is not supported. In some reference type conversions , the compiler cannot determine whether a cast will be valid. It is because of the source type cannot be converted to the destination type , so the cast does not succeed.

How to fix InvalidCastException?

It is important to note that this exception is caused by programming error and should not be handled in a C# try/catch block; instead, the cause of the exception should be eliminated.

C# provides the "is" operator to enable you to test for compatibility before actually performing a cast operation . It will checks if the runtime type of an expression result is compatible with a given type. The "is operator" returns true if the given object is of the same type otherwise, return false. It also returns false for null objects . It's a straightforward extension of the "is" statement that enables concise type evaluation and conversion.





RS Rajapandiyan Settu Syncfusion Team September 15, 2021 01:06 PM UTC

Hi Williamholding, 

Thanks for contacting Syncfusion support. 

Before proceeding with your query, kindly share the below details to validate further on this. 

  1. Share the full Grid code files.
  2. Share the Syncfusion package script version.
  3. When you face the reported issue? (Initial render of Grid/ Doing any Grid action)
  4. Share the video demo of the reported behavior.
  5. If possible, share a simple issue reproducible sample which will be very helpful to resolve the issue earlier.

Also, try the below solution which may resolve the reported problem. 

We suspect that the issue may be raised because of some SerializerSettings. So, we suggest you to add the below code in your startup.cs file to overcome the problem. 

If the ASP.NET CORE version is 2.X then add the below code in startup.cs file 


public void ConfigureServices(IServiceCollection services) 
    services.AddMvc().AddJsonOptions(options => 
    { 
        options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); 
    }); 

 
 
If the ASP.NET CORE version is 3.X then add the below code in startup.cs file 
 


public void ConfigureServices(IServiceCollection services) 
          { 
                      services.AddMvc().AddNewtonsoftJson(options => 
          { 
                 options.SerializerSettings.ContractResolver = 
                    new DefaultContractResolver();
          }); 
             
        } 
 

To use AddNewtonsoftJson in your project we need include the Microsoft.AspNetCore.Mvc.NewtonsoftJson assembly 

 

Note : We need to install NewtonSoft.JSON as dependency since Syncfusion.EJ2.AspNet.Core dependent to NewtonSoft.JSON package. 

Refer to the below documentation for more information. 
 

Regards, 
Rajapandiyan S 


Marked as answer
Loader.
Up arrow icon