简体   繁体   中英

Invalid Cast Exception - cast is not valid

This is the error on my website. No error in Visual Studio, it compiles just fine. This happens when I run the website locally and click the button to show the zones in Chrome or any other browser. I am completely stumped by this and I am looking for any help?? Greatly appreciated.

System.InvalidCastException: Specified cast is not valid.
at MySite.Web.Areas.Mpa.Controllers.ZonesController.d__7.MoveNext() in C:\MySite\Development\routing_branch\src\MySite.Web\Areas\Mpa\Controllers\ZonesController.cs:line 125
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f()

This is the method I am calling which shows line 125:

93 public async Task<PartialViewResult> MapViewModal()
94  {
95      int? impersonatorTenantId;
96      int value;
97      object obj;
98      IZoneAppService zoneAppService = this._zoneAppService;
99      if (this.AbpSession.ImpersonatorTenantId.HasValue)
100     {
101         impersonatorTenantId = this.AbpSession.ImpersonatorTenantId;
102         value = impersonatorTenantId.Value;
103     }
104     else
105     {
106         impersonatorTenantId = this.AbpSession.TenantId;
107         value = impersonatorTenantId.Value;
108     }
109     List<ZoneListDto> zonesByTenantId = await zoneAppService.GetZonesByTenantId(value, true);
110     if (zonesByTenantId == null || zonesByTenantId != null && zonesByTenantId.Count == 0)
111     {
112         zonesByTenantId = new List<ZoneListDto>();
113     }
114     ITenantSettingsAppService tenantSettingsAppService = this._tenantsettingsAppService;
115     if (this.AbpSession.ImpersonatorTenantId.HasValue)
116     {
117         impersonatorTenantId = this.AbpSession.ImpersonatorTenantId;
118         obj = impersonatorTenantId.Value;
119     }
120     else
121     {
122         impersonatorTenantId = this.AbpSession.TenantId;
123         obj = impersonatorTenantId.Value;
124     }
125     string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates((long)obj);
126     ZonesMapView zonesMapView = new ZonesMapView()
127     {
128         Zones = zonesByTenantId,
129         TenantCoordinates = tenantCoordinates
130     };
131     return this.PartialView("_MapViewModal", zonesMapView);
132 }

My table layout is like this:

dbo.MySiteZones
Columns:
Id (PK, bigint, not null)
TenantId (int, not null)
Name (nvarchar(255), not null)
Caption (nvarchar(600), null)
IsActive (bit, not null)
IsDeleted (bit, not null)
DeleterUserId (bigint, null)
DeletionTime (datetime, null)
LastModificationTime (datetime, null)
LastModifierUserId (bigint, null)
CreationTime (datetime, not null)
CreatorUserId (bigint, null)
PolygonCoordinates (nvarchar(max), null)
HexColor (nvarchar(12), null)
PolygonCoordinatesReversed (nvarchar(max), null)

You cannot cast an object directly to long . You should use Convert.ToInt64() which even casts an object value:

string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(Convert.ToInt64(obj));

PS: Why don't you declare 'obj' as long (and change the name accordingly :) ):

long obj;
// ... 
string tenantCoordinates = await tenantSettingsAppService.GetTenantCoordinates(obj);

EDIT Update wrong library call as mentioned by @Jonno

As long as obj is a reference to int you can try explicit cast to int right before casting to long : .GetTenantCoordinates((long)(int)obj)

I think however you can declare obj as int or long .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM