Custom field enum data type throwing cast error


Author
Message
Rob Toyias
Rob Toyias
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 31, Visits: 179
I am receving an error retreving a value from a field that has been customized to map to an enum.  In the BOM I set the ACTIONTYPECODE field to use the InspectionType Field Data Type. No problems there.   I build partial and get the following code in my .Designer file:

[Browsable(false),BusinessFieldDisplayInEditor(),Description("ACTIONTYPECODE"),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public InspectionType ACTIONTYPECODE
{
   
get
  
{
     
return (InspectionType)this.CurrentRow["ACTIONTYPECODE"];
   }
  
set
  
{
     
this.CurrentRow["ACTIONTYPECODE"] = value;
  
}
}

and here is the enum:

public enum InspectionType
{
   Foo= 0,
   Bar= 1,
   Zip= 2
}

Nothing too complex.  Stepping through the cose I can assign a value to ACTIONTYPECODE without issue.  But when I hit the 'Get' I end up with this error:

Cannot unbox 'this.CurrentRow["ACTIONTYPECODE"]' as a 'InspectionType'

Now this would make sense to me since the field seems to be stored as a string and last time I tried I had to do something like this: (InspectionType)Enum.Parse(typeof(InspectionType), "Foo"); to cast a string as an enum.  What am I screwing up? Ermm


Rob Toyias
Rob Toyias
StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)StrataFrame Beginner (33 reputation)
Group: Forum Members
Posts: 31, Visits: 179
I didnt make it very clear in my prev post, I was able to work around this issue by adding the following to the custom code section:

[Browsable(false),BusinessFieldDisplayInEditor(),Description("ACTIONTYPECODE"),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public InspectionType ACTIONTYPECODE
{
   
get
  
{
      return 
(InspectionType)Enum.Parse(typeof(InspectionType), this.CurrentRow["ACTIONTYPECODE"] ); 
   }
  
set
  
{
     
this.CurrentRow["ACTIONTYPECODE"] = value;
  
}
}

This works thus far, I'd just like to know if there is something I'm missing or some larger problem. 
-Thanks

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Thanks.  I am not sure why your project is manifesting itself with that error.  I have tried to reproduce on this side to no avail.  I will let you know if I find anything.  Thanks.
Scott Bressette
Scott Bressette
StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)StrataFrame Beginner (31 reputation)
Group: Forum Members
Posts: 17, Visits: 53
I just had this issue so I figured I'd post the answer on this old entry. It is because there is a type mismatch between your db field and the enum's type. By default Enums are Int types but stingy DBA's like me only use TinyInt's for field that are will be enum values. That translates into a byte type in .Net.



The solution is to change your enum to:



public enum InspectionType : byte

{

Foo= 0,

Bar= 1,

Zip= 2

}



Then it will work correctly.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search