c# - Specified cast is not valid converting INT to ENUM with SqlDataReader -
edit
removing object initializer on assetitem appears have resolved issue, why?
end edit
i don't seem able figure out way read int database cast emun.
i've tried suggestions in both cast int enum in c# , how (efficiently) convert (cast?) sqldatareader field corresponding c# type? without success.
using (var connection = new sqlconnection(_sqlstring)) { using (var command = new sqlcommand("exec getallencodedmedia", connection)) { try { connection.open(); var reader = command.executereader(); while (reader.read()) { if (reader.hasrows) { var assetitem = new mediaservices.encodedassets { id = reader.isdbnull(0) ? 0 : reader.getint32(0), publishedname = reader.isdbnull(1) ? null : reader.getstring(1), publishuri = reader.isdbnull(2) ? new uri("http://www.null.com") : new uri(reader.getstring(2)), encodepreset = reader.isdbnull(3) ? 0 : (mediaservices.encodepresetsforsmoothstreaming)reader.getint32(3), assetid = reader.isdbnull(4) ? null : reader.getstring(4), encoderjobid = reader.isdbnull(5) ? 0 : reader.getint32(5), //encoderstate = reader.isdbnull(6) ? 0 : (jobstate)reader.getint32(6), //assetstate = reader.isdbnull(7) ? 0 : (mediaservices.internalassetstate)reader.getint32(7), groupid = reader.isdbnull(8) ? 0 : reader.getint32(8), published = !reader.isdbnull(2) }; listreturn.add(assetitem); } } } catch (exception ex) { //error } } } [datacontract] public enum encodepresetsforsmoothstreaming { [enummember] [description("h264 smooth streaming 1080p")] h264smoothstreaming1080p, [enummember] [description("h264 smooth streaming 720p")] h264smoothstreaming720p, [enummember] [description("h264 smooth streaming 720p 3g or 4g")] h264smoothstreaming720pfor3gor4g, [enummember] [description("h264 smooth streaming sd 16x9")] h264smoothstreamingsd16x9, [enummember] [description("h264 smooth streaming sd 4x3")] h264smoothstreamingsd4x3 }
how cast ordinal 3 (int) encodepresetsforsmoothstreaming
(enum)?
try declaring enum follows, should allow correctly cast integer correct enum value.
public enum test : int { testvalue1 = 1, testvalue2 = 2, testvalue3 = 3 }
Comments
Post a Comment