0

I have the following enum

public enum Outcome
{
   DontKnow = 0,
   Good = 1,
   Bad = 2,
   NotBad = 3
}

In my Results class, I have a property with this enum:

public class Result
{
   public int Id { get; set; }
   public string Name { get; set; }
   public Outcome FinalOutcome { get; set; }
}

As I read data from the database -- using SqlDataReader -- how do I assign its value?

while(reader.Read())
{
   Id = Convert.IsDbNull(reader[0]) ? Convert.ToInt32(0) : Convert.ToInt32(reader[0]);
   Name = Convert.IsDbNull(reader[1]) ? string.Empty : reader[1].ToString();
   FinalOutcome = Convert.IsDbNull(reader[2]) ? WhatGoesHere? : WhatGoesHere?;
}
Sam
  • 26,817
  • 58
  • 206
  • 383

2 Answers2

3
while(reader.Read())
{
   Id = Convert.IsDbNull(reader[0]) ? Convert.ToInt32(0) : Convert.ToInt32(reader[0]);
   Name = Convert.IsDbNull(reader[1]) ? string.Empty : reader[1].ToString();
   FinalOutcome = Convert.IsDbNull(reader[2]) ? FinalOutcome.DontKnow : (Outcome) Convert.ToInt32(reader[2]);
}
Karanvir Kang
  • 2,179
  • 19
  • 16
0

Assuming your source column is a nullable int:

FinalOutcome = !Convert.IsDbNull(reader[2]) ?(OutCome)reader[2] : (OutCome)0;
Maess
  • 4,118
  • 20
  • 29