"The specified cast is invalid" in an ASP.net MVC application. It works locally and some time after publication, but then throws an error

My MVC application has been working fine for over a year now, but now it starts to cause an error when starting on the Internet. However, it works fine locally in debugging.

I made some changes on Thursday, and it suddenly started to throw an error the following Monday (changes were minor, changes to the layout of the HTML table). The error is intermittent and seems to go away for a while (a day or so) if we restart the server. Any ideas what this could be?

Please let me know if you need more information.

thank

Chris

Server Error in '/ISIS' Application.
--------------------------------------------------------------------------------

Specified cast is not valid. 
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. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[InvalidCastException: Specified cast is not valid.]
   System.Data.SqlClient.SqlBuffer.get_Int32() +4838981
   System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +38
   Read_Target(ObjectMaterializer`1 ) +275
   System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +29
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +7667556
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +61
   ISIS2.Controllers.HomeController.Index() in C:\VS2008\VisualStudio\Projects\ISIS2\ISIS2\Controllers\HomeController.cs:66
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +395
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +145
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

EDIT here is the index code in HomeController:

 public ActionResult Index()
        {
            try
            {
            var user = (Staff)Session["CurrentUser"];

                int reminder = 14;
                if (Request.Form["AlertPeriod"] != null)
                {
                    reminder = int.Parse(Request.Form["AlertPeriod"]);
                }
                if (Request.Form["AlertPeriodLower"] != null)
                {
                    reminder -= 14;
                }
                else if (Request.Form["AlertPeriodHigher"] != null)
                {
                    reminder += 14;
                }
                ViewData["AlertPeriod"] = reminder;
                ViewData["Alerts"] = (from a in db.Alerts
                                      where a.AlertType.Text == "StaffNews" &&
                                      ((a.StartDate >= DateTime.Today && a.StartDate <= DateTime.Today.AddDays(reminder)) ||
                                       (a.EndDate >= DateTime.Today && a.EndDate <= DateTime.Today.AddDays(reminder)) ||
                                       (a.StartDate <= DateTime.Today && a.EndDate >= DateTime.Today.AddDays(reminder)))
                                      orderby a.EndDate
                                      select a).ToList();
                ViewData["Targets"] = (from t in db.Targets
                                       where t.StaffID == user.StaffID &&
                                       ((t.TargetContextType.Description == "StudentSet" && !t.Read) || (t.TargetDiscussions.Any(td => td.AuthorIsStudent && !td.Read)))
                                       orderby (t.Enrolment != null ? t.Enrolment.Course.Name : ""), t.DateDue
                                       select t).ToList();

                ViewData["Trips"] = (from t in db.Trips
                                     where t.TripStartDate >= DateTime.Today && t.TripStartDate <= DateTime.Today.AddDays(14) && t.StaffID == user.StaffID
                                     orderby t.TripStartDate
                                     select t);

                ViewData["Attending"] = (from a in db.TripAttendings
                                         where a.Trip.StaffID == user.StaffID
                                         select a);





                return View("Index", user);
            }
            catch (NullReferenceException e)
            {
                return RedirectToAction("NonUserIndex");
            }
        }
+3
2

, - IIS.

, , , , .

​​ IIS - . , , - .

: . , , , " ". "", ( ).

, Active Result conncetions. , , , , , , , , , . - , , , .

+1

, 66 HomeController SQL- ( Linq), DBNull, Int32

0

Source: https://habr.com/ru/post/1777622/


All Articles