0

I am new to programming so i don't know what i am doing. I am pulling enum value from different class and set them as getter and setter.

 namespace DataLayer.Entities
{
    public enum CourseModeOfDelivery
    {
        Online, ClassRoom, ELearning,
    }
    public class Course
    {
        public int ID { get; set; }
        public String CourseName { get; set; }
        public String Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public CourseModeOfDelivery CourseMode { get; set; }
    }

reading this value in courseRepository

public static Course GetCourse(int id)
        {
            Course a = new Course();
            String GetCommand = "Select CourseName, Description, StartDate, EndDate, CourseMode from Course" + "Where ID = @CourseID";

            SqlConnection connection = DBManager.GetSqlConnection();
            SqlCommand command = new SqlCommand(GetCommand, connection);
            command.Parameters.AddWithValue("@StudentID", id);

            try
            {
                var reader = command.ExecuteReader();

                //Read the Command Object and then return details
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        a.ID = Convert.ToInt32(reader["ID"]);
                        a.CourseName = reader["CourseName"].ToString();
                        a.Description = reader["Description"].ToString();
                        a.StartDate = DateTime.Parse(reader["StartDate"].ToString());
                        a.EndDate = DateTime.Parse(reader["EndDate"].ToString());

                        var selection = CourseModeOfDelivery.ClassRoom;
                        switch (selection)
                        {
                            case CourseModeOfDelivery.ClassRoom:
                                a.CourseMode = CourseModeOfDelivery.ClassRoom;
                                return a.CourseMode;

                            case CourseModeOfDelivery.ELearning:
                                a.CourseMode = CourseModeOfDelivery.ELearning;
                                return a.CourseMode;
                            case CourseModeOfDelivery.Online:
                                a.CourseMode = CourseModeOfDelivery.Online;
                                return a.CourseMode;
                        }
                    a.CourseMode = 
                    }
                }
                else
                {
                    reader.Close();
                }
            }

The requirement is to use switch but don't know how to pull data in there.

1 Answers1

4

It depends on the type of database field you use.

If it is int then:

a.CourseMode = (CourseModeOfDelivery) reader["CourseMode"];

If it is String then:

a.CourseMode = (CourseModeOfDelivery) Enum.Parse(typeof(CourseModeOfDelivery), reader["CourseMode"].toString());

The following might also help you:

Community
  • 1
  • 1
Netzdoktor
  • 526
  • 4
  • 15
  • What do you exactly mean by correct? Your syntax is correct (besides the incomplete assignment after the switch) and the assignments inside the switch should also work. The only strange this is that you set `selection` to a static value and then switch on it. Therefore you will always get the first case statement executed and your switch does only reassigns a value that has previously been set. – Netzdoktor Aug 14 '15 at 08:45
  • i am just trying to save what user has selected from drop down list and save that to a.CourseMode like i did in a.StartDate and a.EndDate and since i user will have 3 choices from drop down to select i have used switch to separate them. – user1669556 Aug 15 '15 at 14:04
  • To answer this, we need more context. How do you get the value from DropDown? Are you using WPF? Where do you edit the user account? The GetUser method presented here seems to only be concerned with reading from a database, right? Where is your code for editing entries? – Netzdoktor Aug 26 '15 at 06:58