开发者

ASP.NET MVC 1 Linq to SQL Error

I have this call:

List<Product> featProducts = 
    productsRepository.Products.Where(x => x.Featured == true).ToList();

It returns the following error: System.InvalidCastException: Specified cast is not valid.

I find it very weird as I am making similar calls in other places and it all works fine. My app compiles with no issues so I do not understand what could be causing that. Below is the detailed stack trace.

Any help would be highly appreciated.

Regards,

Jean-Philippe

Server Error in '/' Application. Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the 开发者_如何学C 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:

Line 50: public ViewResult Featured() Line 51: { Line 52: List featProducts = productsRepository.Products.Where(x => x.Featured == true).ToList(); Line 53: Line 54: foreach (var p in featProducts)

Source File: G:\My Webs\clients\CBP\CBP APP\WebUI\Controllers\ProductsController.cs Line: 52

Stack Trace:

[InvalidCastException: Specified cast is not valid.]

System.Data.SqlClient.SqlBuffer.get_Single() +55

System.Data.SqlClient.SqlDataReader.GetFloat(Int32 i) +38

Read_Product(ObjectMaterializer1 ) +825 System.Data.Linq.SqlClient.ObjectReader2.MoveNext() +29 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +7667540

System.Linq.Enumerable.ToList(IEnumerable1 source) +61

WebUI.Controllers.ProductsController.Featured() in G:\My Webs\clients\CBP\CBP APP\WebUI\Controllers\ProductsController.cs:52 lambda_method(ExecutionScope , ControllerBase , Object[] ) +39

System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17

System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +178

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +24

System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) +192

System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399 System.Web.Mvc.Controller.ExecuteCore() +126 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27

System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7

System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151

Microsoft.Web.Mvc.ViewExtensions.RenderRoute(HtmlHelper helper, RouteValueDictionary routeValues) +214

Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper helper, String actionName, String controllerName, RouteValueDictionary routeValues) +315

Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper helper, String actionName, String controllerName) +10

ASP.views_pages_home_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in g:\My Webs\clients\CBP\CBP APP\WebUI\Views\Pages\home.ascx:12

System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19

System.Web.UI.Control.Render(HtmlTextWriter writer) +10

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19

System.Web.UI.Page.Render(HtmlTextWriter writer) +29

System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016


I have found the issue. It was irrelevant with my query.

It was in the product class. I was trying to match a sql server float with a .net float. It didn't like that. Using a .net double solved the issue.


How about this one ?

List<Product> featProducts = productsRepository.Products.Where(x => x.Featured == true).ToList<Product>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜