//public ActionResult SaveUser(User user) //{ // if (Request.Form["UserId"] == "0") // { // _adminRepository.AddUser(user); // } // else // { // _adminRepository.UpdateUser(user); // } // UserModel vm = new UserModel(); // vm.Users = _adminRepository.getAllusers(); // return View("ManageUsers", vm); //} //public ActionResult ManageUsers() //{ // UserModel vm = new UserModel(); // vm.Users = _adminRepository.getAllusers(); // return View(vm); //} //public ActionResult ChangeUserStatus(int UserId, bool Value) //{ // User obj = new User(); // obj = _adminRepository.getUserById(UserId); // if (obj != null) // { // obj.Active = Value; // } // _adminRepository.UpdateUserStatus (obj); // return Json(new { Message = " User Status Changed Successfully", Success = true }); //} //public ActionResult EditUser(int id) //{ // UserModel vm = new UserModel(); // vm.User = _adminRepository.getUserById(id); // return View("NewUser", vm); //} public JsonResult LoginUser() { UserModel vm = new UserModel(); User obj = new User(); Session["Username"] = null; string username = Request.Form["UserName"]; string password = Request.Form["Password"]; obj = _adminRepository.getUserByName(username, password); if (obj == null) { return Json(new { Message = "Invalid User Name or Password ", Success = false }); } else { if (obj.Userid ==0) { return Json(new { Message = "Invalid User Name or Password ", Success = false }); } else if (obj.Active == false) { return Json(new { Message = "User is Inactive.", Success = false }); } } Session["Username"] = obj; return Json(new { Message = "Login Successfully ", Success = true }); }
public List<User> getAllusers() { const string commandString = @"SELECT Userid, UserName, Active,FirstName,LastName,Password FROM Users "; using (var connection = new SqlConnection(ConnectionString)) { connection.Open(); var command = GetCommand(commandString, connection, CommandType.Text); var users = new List<User>(); { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var user = new User(); user.Userid = (int)reader["Userid"]; user.UserName = (string)reader["UserName"]; user.Password = (string)reader["Password"]; user.FirstName = (string)reader["FirstName"]; user.LastName = (string)reader["LastName"]; user.Active = Convert.ToBoolean(reader["Active"]); users.Add(user); } } } return users; } }
public User getUserByName(string username,string password) { const string commandString = @"SELECT Userid, UserName, Active,FirstName,LastName,Password FROM Users WHERE UserName = @UserName AND Password =@Password"; using (var connection = new SqlConnection(ConnectionString)) { connection.Open(); var command = GetCommand(commandString, connection, CommandType.Text); var user = new User(); { command.Parameters.Add("@UserName", username ); command.Parameters.Add("@Password", password); { using (var reader = command.ExecuteReader()) { while (reader.Read()) { user.Userid = (int)reader["Userid"]; user.UserName = (string)reader["UserName"]; user.Password = (string)reader["Password"]; user.FirstName = (string)reader["FirstName"]; user.LastName = (string)reader["LastName"]; user.Active = Convert.ToBoolean(reader["Active"]); } } } } return user; } }
public void AddUser(User user) { const string commandString = @"Update Users SET Active = @Active WHERE UserId=@UserId"; try { using (var connection = new SqlConnection(ConnectionString)) { connection.Open(); var command = new SqlCommand(commandString, connection); command.Parameters.Add("@UserId", user.Userid); command.Parameters.Add("@Active", user.Active); command.ExecuteNonQuery(); } } catch (Exception ex) { throw new Exception(ex.ToString()); } }