public async Task <RedirectToActionResult> DeleteModeAsync(Guid id) { //get session id (we will use it when updating data and handling errors) String sessionID_s = HttpContext.Session.GetString("Session"); Guid sessionID = Guid.Parse(sessionID_s); Data current = new Data(); Program.data.TryGetValue(sessionID, out current); Mode mode = new Mode(); try { _session.BeginTransaction(); mode = await _session.Modes.Where(b => b.ID.Equals(id)).FirstOrDefaultAsync(); List <Submode> sbmList = await _session.Submode.Where(b => b.mode_id.Equals(id)).ToListAsync(); foreach (Submode s in sbmList) { Scan scan = await _session.Scan.Where(b => b.ID.Equals(s.scan_id)).FirstOrDefaultAsync(); await _session.DeleteScanbyID(scan.ID); } await _session.DeleteMode(id); await _session.Commit(); if (current != null) { current.message = "Mode removed from database"; } } catch (Exception e) { // log exception here if (current != null) { current.message = e.Message.ToString() + " Error"; } await _session.Rollback(); } finally { _session.CloseTransaction(); } if (current != null) { for (int i = 0; i < current.ListOfModes.Count; i++) { if (current.ListOfModes[i].Mode.ID.Equals(id)) { current.ListOfModes.RemoveAt(i); } } } return(RedirectToAction("Edit", "EditRadar", new { id = mode.radar_id })); }