3
Answers

i get error runtime Non-static method requires a target. so how to fix

i work on dotnet application windows form i get error

Error Message : Non-static method requires a target.

Error Type : TargetException

Error Source : mscorlib

Error Trace : at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)

at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)

at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)

at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue)

at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression)

at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)

at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)

at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass43_0.<GetResultsAsync>b__1()

at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__156`1.MoveNext()

--- 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.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__6`1.MoveNext()

--- 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.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__43.MoveNext()

--- 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.Data.Entity.Internal.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__9.MoveNext()

--- 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.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<SingleAsync>d__27`1.MoveNext()

--- 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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

at Locations.Repositories.Repository`1.<Any>d__4.MoveNext() in D:\cars monitoring\CarsMonitoring\CarsMonitoring.Repositories\Repository.cs:line 42

--- 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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

at Locations.Services.CarActionService.<Any>d__4.MoveNext() in D:\cars monitoring\CarsMonitoring\CarsMonitoring.Services\CarActionService.cs:line 46

--- 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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

at CarsMonitoring.frmMain.<tmrCarsActionsMonitor_Tick>d__63.MoveNext() in D:\cars monitoring\CarsMonitoring\CarsMonitoring\frmMain.cs:line 856

         private async void tmrCarsActionsMonitor_Tick(object sender, EventArgs e)
        {
            try
            {
                foreach (var accessControl in zkCurrentAccessBoards)
                {
                    Location location = locations.Where(x => x.Id == accessControl.LocationId).FirstOrDefault();
                    if (!accessControl.IsConnected)
                    {
                        accessControl.IsConnected = accessControl.ConnectByTCP(location.BoardIPAddress, location.BoardSerialPortNumber, location.ComminucationPassword);
                        if (accessControl.IsConnected)
                            AddLog("", $"الموقع [{location.LocationName}] متصل الآن");
                        else
                            continue;
                    }
                    var EventDataSingle = accessControl.ReadEventData();
                    if (EventDataSingle != null)
                    {
                        //tmrCarsActionsMonitor.Stop();
                        CarAction carAction = new CarAction();
                        LocationsDBContext dbContextCarActionService = new LocationsDBContext();
                        IUnitOfWork carActionUnitOfWork = new UnitOfWork(dbContextCarActionService);
                        CarActionService carActionService = new CarActionService(carActionUnitOfWork);
                        LocationsDBContext dbContextCarService = new LocationsDBContext();
                        IUnitOfWork carUnitOfWork = new UnitOfWork(dbContextCarService);
                        CarService carService = new CarService(carUnitOfWork);
                        Car car = null;
                        if (await carService.Any(x => x.CardNo == EventDataSingle.strCardNo))
                        {
                            car = (await carService.GetQuery(x => x.CardNo == EventDataSingle.strCardNo)).FirstOrDefault();
                            if (car != null)
                                carAction.CarId = car.Id;
                        }
                        DateTime.TryParse(EventDataSingle.strTime, out DateTime dtActionDate);
                        if (accessControl.LocationTypeId == 1)
                        {
                            carAction.CardNo = EventDataSingle.strCardNo;
                            carAction.SourceLocationId = accessControl.LocationId;
                            carAction.SourceActionDate = dtActionDate;
                        }
                        else
                        {
                            carAction = (await carActionService.Get(x => x.CardNo == EventDataSingle.strCardNo && x.TargetLocationId == null, null, 0, 1, false)).LastOrDefault();
                            if (await carActionService.Any(x => x.Id > carAction.Id && x.CardNo == EventDataSingle.strCardNo))
                                carAction = null;
                            if (carAction == null)
                            {
                                carAction = new CarAction
                                {
                                    CardNo = EventDataSingle.strCardNo
                                };
                            }
                            carAction.TargetLocationId = accessControl.LocationId;
                            carAction.TargetActionDate = dtActionDate;
                        }
                        if (carAction.Id == 0)
                        {
                            await carActionService.Add(carAction);
                            if (carAction.SourceLocationId > 0 && (carAction.TargetLocationId == null || carAction.TargetLocationId <= 0))
                            {
                                string strImagePath = $@"{System.Configuration.ConfigurationManager.AppSettings["ImagesSavePath"].ToString()}\S{carAction.Id}.jpg";
                                WebClientCameraService.CaptureImage(location.CameraUrl, strImagePath, location.CameraUserName, location.CameraPassword);
                                carAction.SourceImage = strImagePath;
                                if (car == null)
                                    AddLog(carAction.CardNo, "تم تحميل سيارة غير مسجلة");
                                else
                                    AddLog(carAction.CardNo, $"تم تحميل سيارة رقم [{car.CarNo}]");
                            }
                            else
                            {
                                string strImagePath = $@"{System.Configuration.ConfigurationManager.AppSettings["ImagesSavePath"].ToString()}\T{carAction.Id}.jpg";
                                WebClientCameraService.CaptureImage(location.CameraUrl, strImagePath, location.CameraUserName, location.CameraPassword);
                                carAction.TargetImage = strImagePath;
                                if (car == null)
                                    AddLog(carAction.CardNo, "تم تفريغ سيارة غير مسجلة");
                                else
                                    AddLog(carAction.CardNo, $"تم تفريغ سيارة رقم [{car.CarNo}]");
                            }
                            await carActionService.Update(carAction, carAction.Id);
                        }
                        else
                        {
                            string strImagePath = $@"{System.Configuration.ConfigurationManager.AppSettings["ImagesSavePath"]}\T{carAction.Id}.jpg";
                            WebClientCameraService.CaptureImage(location.CameraUrl, strImagePath, location.CameraUserName, location.CameraPassword);
                            carAction.TargetImage = strImagePath;
                            await carActionService.Update(carAction, carAction.Id);
                            if (car == null)
                                AddLog(carAction.CardNo, "تم تفريغ سيارة غير مسجلة");
                            else
                                AddLog(carAction.CardNo, $"تم تفريغ سيارة رقم [{car.CarNo}]");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.LogErrorSilently(ex);
                AddLog("0", ex.Message);
            }
            finally
            {
                //tmrCarsActionsMonitor.Start();
            }
        }

how to fix issue and what is reason of issue

Answers (3)