public void UpdateUser(User user) { using (var cn = new SqlConnection(_connectionString)) { cn.Open(); cn.Execute("update Users set UserName = @UserName, PopupRefreshTimeInMinutes = @PopupRefreshTimeInMinutes, IsActive = @IsActive where UserID = @UserID", new { user.Username, user.PopupRefreshTimeInMinutes, user.IsActive, user.UserID }); cn.Close(); } }
public void CreateUser(User user) { using (var cn = new SqlConnection(_connectionString)) { cn.Open(); cn.Execute("insert Users (UserName, PopupRefreshTimeInMinutes, IsActive) values (@UserName, @PopupRefreshTimeInMinutes, @IsActive)", new { user.Username, user.PopupRefreshTimeInMinutes, user.IsActive }); cn.Close(); } }
public ActionResult Edit(User user) { try { new UserDB().UpdateUser(user); return RedirectToAction("Index"); } catch { return View(user); } }
public ActionResult Delete(User user) { try { new UserDB().DeleteUser(user); return RedirectToAction("Index"); } catch(Exception e) { ViewBag.ErrorText = e.Message; return View(user); } }
public void DeleteUser(User user) { using (var cn = new SqlConnection(_connectionString)) { cn.Open(); //check to make sure the user isn't referenced by log entries if (cn.Query("select * from TaskLogEntries where UserID = @UserID", new { user.UserID }).Any()) { cn.Close(); throw new Exception(String.Format("Unable to delete the user, {0}, because they are referenced in task log entries", user.Username)); } cn.Execute("delete from Users where UserID = @UserID", new { user.UserID }); cn.Close(); } }