public ActionResult UpdateUser(UserVM changeUser) { ActionResult oResult = null; if (Session["UserName"] != null && (int)Session["UserLevel"] == 1) { //if the info was entered as required if (ModelState.IsValid) { try { IuserInfoDO updateUser = UserMap.MapPOtoDO(changeUser.User); //call to producer data access method _UserAccess.UpdateUser(updateUser); } catch (Exception e) { //what to do with exception, what to write if (e.Message.Contains("duplicate")) { changeUser.ErrorMessage = "Unable to process request, duplicate record already exists"; } else { changeUser.ErrorMessage = "We apologize but we were unable to handle your request at this time."; } } finally { } if (changeUser.ErrorMessage == null) { //if no errors take them to producer list to see if their submission was added oResult = RedirectToAction("ViewUsers", "User"); } else { //if any errors caught then redirect to screen to change producer info, same as they came from oResult = View(changeUser); } } else { oResult = View(changeUser); } } else { oResult = RedirectToAction("Login", "User"); } return(oResult); }
//mapping from data class to presentation class so that we can use this object on different layers public static UserPO MapDOtoPO(IuserInfoDO iFrom) { //instantiate new Presentation object to fill from Data object UserPO newuser = new UserPO(); newuser.UserID = iFrom.UserID; newuser.FirstName = iFrom.FirstName; newuser.LastName = iFrom.LastName; newuser.UserName = iFrom.UserName; newuser.Password = iFrom.Password; newuser.UserLevel = iFrom.UserLevel; return(newuser); }
public ActionResult AddUser(UserVM viewModel) { ActionResult oResponse = null; if (ModelState.IsValid) { try { IuserInfoDO userform = UserMap.MapPOtoDO(viewModel.User); //call to User DAL for method _UserAccess.InsertUser(userform); //set tempdata to hold registered info for login screen TempData["UserName"] = userform.UserName; TempData["Password"] = userform.Password; } catch (Exception e) { if (e.Message.Contains("duplicate")) { //if duplicate user name is found, give user this msg viewModel.ErrorMessage = String.Format("There is already a {0} in the database.", viewModel.User.UserName); } else { //all other errors return this msg to user viewModel.ErrorMessage = "We apologize but we were unable to handle your request at this time."; } } finally { //nothing to do here } if (viewModel.ErrorMessage == null) { oResponse = RedirectToAction("Login", "User"); } else { oResponse = View(viewModel); } } else { oResponse = View(viewModel); } return(oResponse); }
public ActionResult UpdateUser(int UserID) { if (Session["UserName"] != null && (int)Session["UserLevel"] == 1) { UserVM updateVM = new UserVM(); IuserInfoDO user = _UserAccess.ViewUserByID(UserID); updateVM.User = UserMap.MapDOtoPO(user); return(View(updateVM)); } else { return(RedirectToAction("Login", "User")); } }
public void InsertUser(IuserInfoDO user) { try { //create connection using (SqlConnection connectionToSQL = new SqlConnection(connectionString)) //create a command using (SqlCommand command = new SqlCommand("ADD_USER", connectionToSQL)) { try { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 35; command.Parameters.AddWithValue("@FirstName", user.FirstName); command.Parameters.AddWithValue("@LastName", user.LastName); command.Parameters.AddWithValue("@UserName", user.UserName); command.Parameters.AddWithValue("@Password", user.Password); connectionToSQL.Open(); command.ExecuteNonQuery(); } catch (Exception e) { ErrorLogging.LogError(e); throw (e); } finally { connectionToSQL.Close(); connectionToSQL.Dispose(); } } } catch (Exception e) { ErrorLogging.LogError(e); throw (e); } finally { //no connection to dispose, do nothing } }
public void UpdateUser(IuserInfoDO user) { try { //create connection using (SqlConnection connectionToSQL = new SqlConnection(connectionString)) //create a command using (SqlCommand command = new SqlCommand("UPDATE_USER", connectionToSQL)) { try { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 35; command.Parameters.AddWithValue("@UserID", user.UserID); command.Parameters.AddWithValue("@FirstName", user.FirstName); command.Parameters.AddWithValue("@LastName", user.LastName); command.Parameters.AddWithValue("@UserName", user.UserName); command.Parameters.AddWithValue("@Password", user.Password); command.Parameters.AddWithValue("@UserLevel", user.UserLevel); connectionToSQL.Open(); command.ExecuteNonQuery(); } catch (Exception e) { ErrorLogging.LogError(e); throw e; } finally { } } } catch (Exception e) { throw e; } finally { //good to go } }