/// <summary> /// Maps all munitions by row -- called in ViewMunitions() /// </summary> /// <param name="dataRow"></param> /// <returns></returns> public MunitionsDO MapAllMunitions(DataRow dataRow) { //Instantiating new MunitionsDO MunitionsDO munitions = new MunitionsDO(); try { //Checking to make sure row exists and is not null if (dataRow.Table.Columns.Contains("TopicID") && dataRow["TopicID"] != DBNull.Value) { munitions.TopicID = (Int64)dataRow["TopicID"]; } munitions.MunitionID = (Int64)dataRow["MunitionID"]; munitions.Munition = dataRow["Munition"].ToString(); munitions.Description = dataRow["Description"].ToString(); } catch (Exception ex) { //Error logging MunitionsErrorHandler("error", "MunitionsDAO", "MapAllMunitions", ex.Message, ex.StackTrace); throw ex; } //returning munitions return(munitions); }
/// <summary> /// Updates munition -- called in Controller Post Update() /// </summary> /// <param name="munitions"></param> public void UpdateMunitions(MunitionsDO munitions) { try { //Instantiating SqlConnection using (SqlConnection conn = new SqlConnection(connectionString)) { //Creating a SqlCommand to use a stored procedure SqlCommand cmd = new SqlCommand("UPDATE_MUNITION", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@MunitionID", munitions.MunitionID); cmd.Parameters.AddWithValue("@Munition", munitions.Munition); cmd.Parameters.AddWithValue("@Description", munitions.Description); //Open connection and execute nonquery conn.Open(); cmd.ExecuteNonQuery(); } } catch (SqlException ex) { //Error logging MunitionsErrorHandler("error", "MunitionsDAO", "UpdateMunitions", ex.Message, ex.StackTrace); throw ex; } }
public ActionResult Update(MunitionsPO form) { //Redirection to Index page for Munitions ActionResult oResponse = RedirectToAction("Index", "Munitions", new { form.TopicID }); //Checking for valid modelstate if (ModelState.IsValid) { try { //Mapping form objects from PO to DO MunitionsDO dataObject = MunitionsMapper.MapPOtoDO(form); //Passing objects to UpdateMunitions() dataAccess.UpdateMunitions(dataObject); TempData["Message"] = $"Successfully updated {form.Munition}"; } catch (Exception ex) { //Error message oResponse = View(form); TempData["ErrorMessage"] = "Update was unsuccessful"; } } //ModelState not valid else { //User filled form out incorrectly oResponse = View(form); } //Return redirect ActionResult return(oResponse); }
//Mapping one Munition from DO to PO public static MunitionsPO MapDOtoPO(MunitionsDO from) { MunitionsPO to = new MunitionsPO(); to.MunitionID = from.MunitionID; to.Munition = from.Munition; to.Description = from.Description; to.TopicID = from.TopicID; //Returning Munition return(to); }
//Mapping list of Munitions from PO to DO public static List <MunitionsDO> MapPOtoDO(List <MunitionsPO> from) { List <MunitionsDO> to = new List <MunitionsDO>(); if (from != null) { foreach (MunitionsPO munition in from) { //Changing a PO munition to a DO munition MunitionsDO mappedMunition = MapPOtoDO(munition); //Adding DO munition to a list of DO munitions to.Add(mappedMunition); } } //Returning list of Munitions return(to); }
/// <summary> /// View Munitions by TopicID -- called in Controller Index() /// </summary> /// <param name="topicID"></param> /// <returns></returns> public List <MunitionsDO> ViewMunitions(Int64 topicID) { //Intantiating new list to return munitions List <MunitionsDO> munitions = new List <MunitionsDO>(); try { //Instantiating SqlConnection using (SqlConnection conn = new SqlConnection(connectionString)) { //Creating a SqlCommand to use a stored procedure. SqlCommand cmd = new SqlCommand("DISPLAY_MUNITIONS_BY_TOPIC", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@TopicID", topicID); conn.Open(); //Using SqlDataAdapter to get SQL table. DataTable munitionInfo = new DataTable(); using (SqlDataAdapter munitionAdapter = new SqlDataAdapter(cmd)) { munitionAdapter.Fill(munitionInfo); munitionAdapter.Dispose(); } //Putting datarow into a list. foreach (DataRow row in munitionInfo.Rows) { MunitionsDO mappedMunitions = MapAllMunitions(row); munitions.Add(mappedMunitions); } } } catch (Exception ex) { //Error logging MunitionsErrorHandler("error", "MunitionsDAO", "ViewMunitions", ex.Message, ex.StackTrace); throw ex; } //Returning list of munitions return(munitions); }
/// <summary> /// Viewing munitions by their ID -- called in Controller Get Update() /// </summary> /// <param name="munitionID"></param> /// <returns></returns> public MunitionsDO ViewMunitionsByID(Int64 munitionID) { //Instantiating munitionsDO MunitionsDO munitions = new MunitionsDO(); try { //Instantiating SqlConnection using (SqlConnection conn = new SqlConnection(connectionString)) { //Creating a new SqlCommand to use a stored procedure. SqlCommand cmd = new SqlCommand("DISPLAY_MUNITIONS_BY_ID", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@MunitionID", munitionID); conn.Open(); //Using SqlDataAdapter to get SQL table. DataTable munitionInfo = new DataTable(); using (SqlDataAdapter munitionAdapter = new SqlDataAdapter(cmd)) { munitionAdapter.Fill(munitionInfo); munitionAdapter.Dispose(); } //Mapping row to munitions foreach (DataRow row in munitionInfo.Rows) { munitions = MapAllMunitions(row); } } } catch (Exception ex) { //Error Logging MunitionsErrorHandler("error", "MunitionsDAO", "ViewMunitionsByID", ex.Message, ex.StackTrace); throw ex; } //returning munition's info by munitionID return(munitions); }
public ActionResult Update(Int64 topicID, Int64 munitionID) { //Viewing munition with specific munitionID selected ActionResult oResponse = View(munitionID); try { //Getting munition by ID from data layer and mapping to presentation layer MunitionsDO munitions = dataAccess.ViewMunitionsByID(munitionID); MunitionsPO displayObject = MunitionsMapper.MapDOtoPO(munitions); //Setting topic ID to current topic displayObject.TopicID = topicID; oResponse = View(displayObject); } catch (Exception ex) { //Error message TempData["Message"] = "Munition could not be updated at this time."; } //Return ActionResult return(oResponse); }