Unable to cast object of type ‘System.Int16’ to my Enum variable [duplicate]

I'm tryng to convert a numeric type on my mysql database (smallint(6)) and I'm getting the following error:

Unable to cast object of type 'System.Int16' to type 'MyEnum'.

My Enum is:

public enum MyEnum
    Value1 = 1,
    Value2 = 2,
    Value3 = 3,
    Value4 = 4

The conversion is happening here:

MyEnum enumHere = (MyEnum)reader["cod_sist_orig"];

Reader is a IDataReader and the column is a not nullable column and the value comming from the database is 1.

Read more here: https://stackoverflow.com/questions/67928693/unable-to-cast-object-of-type-system-int16-to-my-enum-variable

Content Attribution

This content was originally published by Felipe Deveza at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: