public List <OptionModel> FetchOptions(int qno) { var query = "select * from options where QuestionId=@id"; var cmd = createCommand(query); cmd.Parameters.AddWithValue("@id", qno); try { cmd.Connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); List <OptionModel> options = new List <OptionModel>(); while (reader.Read()) { var option = new OptionModel(); option.OptionId = Convert.ToInt32(reader["OptionId"]); option.QuestionId = Convert.ToInt32(reader["QuestionId"]); option.LinkId = Convert.ToInt32(reader["LinkId"]); option.MonitorId = Convert.ToInt32(reader["MonitorId"]); option.OptionText = reader["OptionText"].ToString(); options.Add(option); } return(options); } catch (Exception ex) { throw ex; } finally { cmd.Connection.Close(); } }
public int Process() { int qno = 1; OptionModel optionSelected = null; while (qno != 0) { QuestionModel question = FetchQuestion(qno); Console.WriteLine(question.QuestionText); List <OptionModel> options = FetchOptions(qno); foreach (var option in options) { Console.WriteLine("Select an option"); Console.WriteLine("{0}:{1}", option.OptionId, option.OptionText); } bool valid = false; int option_choosen = 0; while (!valid) { var input = Console.ReadLine(); if (int.TryParse(input, out option_choosen)) { if (option_choosen > 0 && option_choosen <= options.Count()) { valid = true; } else { Console.WriteLine("!!!! Choose the Valid Option !!!!\n"); } } else { Console.WriteLine("!!!! Choose the Valid Option !!!!\n"); } } optionSelected = (options.Where(c => c.OptionId == option_choosen).FirstOrDefault()); qno = optionSelected.LinkId; } return(optionSelected.MonitorId); }