InvalidCastException with LLBLGenProDataSource and Enum primary key

Posts   
 
    
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 28-Sep-2012 11:37:46   

I have a lookup table with an Enum as primary key and when I tried to use in in a master/detail situation with a LLBLGenProDataSource I got this exception:

Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'.

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: Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'.

Source Error: 


Line 577:
Line 578:                   // pick first, instantiate the typeconverter
Line 579:                   TypeConverterAttribute attributeToUse = (TypeConverterAttribute)typeConverterAttributes[0];
Line 580:                   if(attributeToUse.ConverterTypeName.Length <= 0)
Line 581:                   {

Source File: c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceViewBase.cs    Line: 579 

Stack Trace: 


[InvalidCastException: Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'.]
   System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +14018474
   System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14018558
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceViewBase.ConvertValueToDestinationType(Type destinationType, Object sourceValue, String fieldName) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceViewBase.cs:579
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.CreateRuntimeFilter() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceView.cs:254
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ProduceFilterToUse(IPredicateExpression baseFilter) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceView.cs:851
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ExecuteSelectEntityCollection(Int32 pageSize, Int32 pageNumber, DataSourceSelectArguments arguments) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceView.cs:600
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceView.cs:529
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +28
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +313
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +146
   System.Web.UI.WebControls.FormView.EnsureDataBound() +99
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +192
   System.Web.UI.Control.EnsureChildControls() +189
   System.Web.UI.Control.PreRenderRecursiveInternal() +60
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4297

To repro: download the ASP.NET 2.0 databinding example http://www.llblgen.com/pages/secure/filestreamer.aspx?DownloadID=220 change CategoryEntity.CategoryId to an Enum e.g

public enum Category
  {
    Beverages = 1,
    Condiments,
    Confections,
    Dairy,
    Grains,
    Meat,
    Produce,
    Seafood
  }

regen,compile,run navigate to /ProductsDetails.aspx

Happens with adapter/selfservicing v3.1 and v3.5

Jeremy Thomas
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Sep-2012 11:54:08   

Which 3.5 runtime library version (build number) are you using? (check the forum guidelines).

TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 03-Oct-2012 13:37:15   

Walaa wrote:

Which 3.5 runtime library version (build number) are you using? (check the forum guidelines).

SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll version 3.5.12.0807 SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll version 3.5.12.0824

Jeremy Thomas
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Oct-2012 06:41:07   

Can't reproduce it. I'm attaching my repro.

Visual Studio 2010 .NET Fremwork 4.0 LLBLGenPro_NET40.exe (v.3.5) latest

Make sure the DBGeneric Project is referencing the correct Enum assembly. Same one mentioned in the .typeimports file.

If it doesn't work, rebuild the solution, create a new aspx page, and drop the LLBLGenroDataSource.

Attachments
Filename File size Added on Approval
Northwind.rar 306,541 04-Oct-2012 06:41.25 Approved
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 04-Oct-2012 10:46:07   

Something's wrong with your references I think. Check the stacktrace: SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceViewBase.ConvertValueToDestinationType(Type destinationType, Object sourceValue, String fieldName) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceViewBase.cs:579

Frans Bouma | Lead developer LLBLGen Pro
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 04-Oct-2012 11:25:39   

Walaa wrote:

Can't reproduce it. I'm attaching my repro.

Where is the master/detail in the project?

Jeremy Thomas
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 04-Oct-2012 11:47:42   

Otis wrote:

Something's wrong with your references I think. Check the stacktrace: SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceViewBase.ConvertValueToDestinationType(Type destinationType, Object sourceValue, String fieldName) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.1\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\DataSource\LLBLGenProDataSourceViewBase.cs:579

As i said it happens in 3.1 and 3.5 the trace was obviously from 3.1 here is the 3.5 version


Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'. 
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: Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'.

Source Error: 

Line 577:
Line 578:                   // pick first, instantiate the typeconverter
Line 579:                   TypeConverterAttribute attributeToUse = (TypeConverterAttribute)typeConverterAttributes[0];
Line 580:                   if(attributeToUse.ConverterTypeName.Length <= 0)
Line 581:                   { 

Source File: c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceViewBase.cs   Line: 579 

Stack Trace: 

[InvalidCastException: Invalid cast from 'System.String' to 'SD.LLBLGen.Pro.Examples.Category'.]
   System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +14018474
   System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14018558
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceViewBase.ConvertValueToDestinationType(Type destinationType, Object sourceValue, String fieldName) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceViewBase.cs:579
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.CreateRuntimeFilter() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceView.cs:254
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ProduceFilterToUse(IPredicateExpression baseFilter) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceView.cs:851
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ExecuteSelectEntityCollection(Int32 pageSize, Int32 pageNumber, DataSourceSelectArguments arguments) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceView.cs:600
   SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses.Web\LLBLGenProDataSourceView.cs:529
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +28
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +313
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +146
   System.Web.UI.WebControls.FormView.EnsureDataBound() +99
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +192
   System.Web.UI.Control.EnsureChildControls() +189
   System.Web.UI.Control.PreRenderRecursiveInternal() +60
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4297

Jeremy Thomas
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 04-Oct-2012 14:43:17   

I think (but have to check) the problem is that the value selected in the combo box is a string value, not an int value (the combo box thus has the enum value as string in both display value and actual value). There's no code in the datasource control for converting the string value of an enum to an enum instance.

Will check whether this is indeed the case.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 04-Oct-2012 15:34:35   

The problem occurs because the combo box at the top to filter the products is filled with enum values, which result in strings in the 'Value' field of the combo box:


<table class="contentHeader">
                    <tr>
                        <td>Product category:</td>
                        <td align="left"><select name="ctl00$ContentPlaceHolder1$CategoriesList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$CategoriesList\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_CategoriesList" style="width:155px;">
        <option selected="selected" value="Beverages">Beverages</option>
        <option value="Condiments">Condiments</option>
        <option value="Confections">Confections</option>
        <option value="Dairy">Dairy Products</option>
        <option value="Grains">Grains/Cereals</option>
        <option value="Meat">Meat/Poultry</option>
        <option value="Produce">Produce</option>
        <option value="Seafood">Seafood</option>

    </select>

This happens because the 'value' field is CategoryId, which is an enum field. This value is then sent as value to use for the filter, which is tried to be converted to an enum, which fails, as there's no code to convert the string value of an enum to the enum value itself.

So as it is a general PAIN to make this produce int values at the 'value' field, it is better to add enum conversion for string -> enum value in the datasourcecontrol. Will do that (v3.5)

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 04-Oct-2012 15:55:04   

Fixed. Please see the attached dll. (3.5)

Attachments
Filename File size Added on Approval
SD.LLBLGen.Pro.ORMSupportClasses.Web.dll 77,312 04-Oct-2012 15:55.10 Approved
Frans Bouma | Lead developer LLBLGen Pro
TomDog
User
Posts: 623
Joined: 25-Oct-2005
# Posted on: 05-Oct-2012 09:33:40   

Otis wrote:

Fixed. Please see the attached dll. (3.5)

Yep-sorted thankssmile

Jeremy Thomas