InvalidCastException Errors happening

Discussion in 'ASP.NET / ASP.NET Core' started by g3misa, Dec 22, 2015.

  1. Hi,

    Just tonight i am seeing a lot of 'Specified cast is not valid' errors on all IQueryable<> .ToList() calls in my code. I haven't changed anything in my code. This was working for 5 years until tonight. Did DiscountASP.net change anything??

    {
    "Message": "Specified cast is not valid.",
    "StackTrace": " at System.Data.SqlClient.SqlBuffer.get_Int16()\r\n at System.Data.SqlClient.SqlDataReader.GetInt16(Int32 i)\r\n at Read_Client(ObjectMaterializer`1 )\r\n at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Web.Modules.Admin.Services.Admin.SearchClient(String info)",
    "ExceptionType": "System.InvalidCastException"
    }
     
  2. Never mind. My bad. I changed something on the database from smallint to int.
     
    RayH, martino and mjp like this.
  3. 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.It is important to note that this exception is caused by programming error and should not be handled in a 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.
     

Share This Page