// GET: CohortOne public ActionResult Index() { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT c.Id, c.Name FROM CohortOne c"; SqlDataReader reader = cmd.ExecuteReader(); List <CohortOne> cohortOnes = new List <CohortOne>(); while (reader.Read()) { CohortOne cohortOne = new CohortOne { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")) }; cohortOnes.Add(cohortOne); } reader.Close(); return(View(cohortOnes)); } } }
private CohortOne GetCohortOneById(int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT c.Id, c.Name FROM CohortOne c WHERE c.Id=@id "; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); CohortOne cohortOne = null; if (reader.Read()) { cohortOne = new CohortOne { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")) }; } reader.Close(); return(cohortOne); } } }
public ActionResult Edit(int id, CohortOneEditViewModel viewModel) { CohortOne cohortOne = viewModel.cohorts; try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" UPDATE CohortOne SET Id = @id, Name = @Name Where Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.Parameters.Add(new SqlParameter("@Name", cohortOne.Name)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
// GET: CohortOne/Edit/5 public ActionResult Edit(int id) { CohortOne cohortOne = GetCohortOneById(id); List <CohortOne> cohorts = GetAllCohortOnes(); CohortOneEditViewModel viewModel = new CohortOneEditViewModel(); viewModel.cohorts = cohortOne; viewModel.AvailableCohorts = cohorts; return(View(viewModel)); }
// GET: CohortOne/Details/5 public ActionResult Details(int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT c.Id, c.Name FROM CohortOne c Where Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); CohortOne cohortOne = null; if (reader.Read()) { cohortOne = new CohortOne { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")) }; reader.Close(); return(View(cohortOne)); } else { return(new StatusCodeResult(StatusCodes.Status404NotFound)); } } } }
// GET: CohortOne/Delete/5 public ActionResult Delete(int id) { CohortOne cohortOne = GetCohortOneById(id); return(View(cohortOne)); }