Why am I getting a Specified Cast is not valid error?
I've developed an application with ASP.NET MVC 2, and after deploying it, I get an InvalidCastException
:
Error/Exception: "Specified cast is not valid."
Stacktrace:
[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Time() +77
System.Data.SqlClient.SqlDataReader.GetTimeSpan(Int32 i) +56
Read_Question(ObjectMaterializer`1 ) +1740
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
TestEnvironment.Managements.QuestionManager.GetquestionsByTestId(Int32 testId) in D:\ParallelMinds\Projects\TestApplication\TestEnvironment\TestEnvironment\Managements\QuestionManager.cs:131
TestEnvironment.Controllers.LoadTestController.Index(Nullable`1 testId) in D:\ParallelMinds\Projects\TestApplication\TestEnvironment\TestEnvironment\Controllers\LoadTestController.cs:31
lambda_method(ExecutionScope , ControllerBase , Object[] ) +86
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__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +53
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +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) +300
System.Web.Mvc.Controller.ExecuteCore() +104
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +36
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<开发者_Python百科MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +53
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +30
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8681102
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Why am I getting this error only when I deploy the application? It works fine in my local development server, and I only get this exception on one page.
Without any more details I would guess that the table you are accessing on your deployment database is inconsistent with the table on your development database.
Maybe you have a column that is of a different type on your local machine.
Either that or there is some invalid data in the record you are retrieving in the deployment database.
The error Specified cast is not valid
is encountered when one type cannot be directly cast to another.
Usually, this mistake is made when the programmer thinks they have one type, but actually have another. Take for example a textbox which has been constrained to only accept numbers. A programmer might well read the value, and later on make the mistake of expecting it to be a number
string myValue = myTextBox.Text;
// later
someComponent.ValueExpectingANumber = (int)myValue; // specified cast is not valid
in the above example, the number would have to be "Parsed" rather than cast.
It looks to me like something in your Read_Question
method is filling in a field that's supposed to be SQL-compatible date or time value, and whatever the method is putting into the value is improperly formatted. The System.Data.SqlClient.SqlBuffer.get_Time()
method tries to convert that value to a SQL date/time and throws an exception.
The first place I would look is in the Read_Question
method.
If this happened when you published the DB, it could be that the culture is different for that server instance. For example, the differences in the english and american versions of the date.
Alternatively, if you are using DateTime.MinValue, the DateTime.MinValue differs from the range accepted by sql server; instead use the SqlDateTime object.
Can you post the code that is calling this method?
Matt
精彩评论