2009-12-02 6 views
1

J'ai cet appel:ASP.NET MVC 1 LINQ to SQL erreur

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

Il renvoie l'erreur suivante: System.InvalidCastException: spécifié est pas valide. Je trouve cela très étrange car je fais des appels similaires dans d'autres endroits et tout fonctionne très bien. Mon application compile sans problèmes, donc je ne comprends pas ce qui pourrait causer cela. Voici la trace détaillée de la pile.

Toute aide serait grandement appréciée.

Cordialement,

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 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(ObjectMaterializer 1) +825 System.Data.Linq.SqlClient.ObjectReader 2.MoveNext() +29 System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) +7667540
System.Linq.Enumerable.ToList(IEnumerable 1 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, 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() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary 2 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

Répondre

3

J'ai trouvé le problème. Ce n'était pas pertinent avec ma requête.

C'était dans la classe de produit. J'essayais de faire correspondre un flottant de serveur sql avec un float de .net. Ça n'a pas plu à ça. L'utilisation d'un .net double a résolu le problème.

0

Que diriez-vous celui-ci?

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

De COurse. Je ne peux pas croire que je n'ai pas vu ça. Vous savez quand vous avez lu la même ligne tant de fois. Merci beaucoup. Jean-Philippe – user208250

+0

Je me suis un peu trop excité là-bas. Pardon. Il est toujours erroné. – user208250

+0

toujours la même erreur? –