示例#1
0
        public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            string username = model.Username;
            string password = model.Password;
            string name = model.Name;
            string email = model.Email;
            DateTime date = DateTime.Now;
            string userType = "DF";

            string errormsg = null;

            UserInfoTableAdapter UserInfoAdapter = new UserInfoTableAdapter();
            DataTable UserInfoDT = UserInfoAdapter.GetDataByUsername(username);

            if (UserInfoDT.Rows.Count == 1)
            {
                errormsg += "Tên đăng nhập ";
            }

            if (!string.IsNullOrEmpty(email))
            {
                AccountTableAdapter AccountAdapter = new AccountTableAdapter();
                DataTable AccountDT = AccountAdapter.GetDataByEmail(email);

                if (AccountDT.Rows.Count == 1)
                {
                    if (errormsg != null)
                    {
                        errormsg += ", Email ";
                    }
                    else errormsg += "Email ";
                }
            }

            if (errormsg != null)
            {
                errormsg += "đã tồn tại!";
                ModelState.AddModelError("", errormsg);
                return View(model);
            }
            else
            {
                AccountTableAdapter AccountAdapter = new AccountTableAdapter();

                UserInfoAdapter.Connection.Open();
                AccountAdapter.Connection = UserInfoAdapter.Connection;

                using (SqlTransaction transaction = UserInfoAdapter.Connection.BeginTransaction())
                {
                    UserInfoAdapter.AttachTransaction(transaction);
                    AccountAdapter.AttachTransaction(transaction);

                    try
                    {
                        UserInfoAdapter.InsertUserInfo(username, name, userType, 0, date, null, null, null, false, false, date, username, date);
                        Log.ActivityLog("Insert into UserInfo: Username = "******"Insert into Account: Username = "******"register"] = "Đăng ký thành công!";
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Log.ErrorLog(ex.Message);
                        Session["register"] = "Đăng ký thất bại!";
                    }
                }
            }

            return RedirectToAction("HomePage", "Home");
        }
示例#2
0
        public ActionResult EditUser(EditUserModel model)
        {
            UserTypeTableAdapter userTypeAdapter = new UserTypeTableAdapter();
            DataTable userTypeDT = userTypeAdapter.GetData();

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (DataRow row in userTypeDT.Rows)
            {
                items.Add(new SelectListItem { Text = row["TypeName"].ToString(), Value = row["TypeShortName"].ToString() });
            }
            ViewData["UserType"] = items;

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string email = model.Email;
            if (!string.IsNullOrEmpty(email))
            {
                AccountTableAdapter AccountAdapter = new AccountTableAdapter();
                DataTable dt = AccountAdapter.GetDataByUsername(model.Username);

                if (!dt.Rows[0]["Email"].ToString().Equals(email))
                {
                    DataTable AccountDT = AccountAdapter.GetDataByEmail(email);

                    if (AccountDT.Rows.Count == 1)
                    {
                        if (!AccountDT.Rows[0]["Username"].ToString().Equals(model.Username))
                        {
                            Log.ActivityLog(AccountDT.Rows[0]["Username"].ToString().Equals(model.Username).ToString());
                            ModelState.AddModelError("", "Email đã tồn tại");
                            return View(model);
                        }
                    }
                }
            }

            string updateBy = AccountInfo.GetUserName(Request);
            DateTime date = DateTime.Now;
            string username = model.Username;
            string name = model.Name;
            string userTypeID = model.UserTypeID;
            bool isCafeteriaStaff = false;
            int role = model.Role;
            bool isActive = model.isActive;
            if (role == 2)
            {
                isCafeteriaStaff = true;
            }

            AccountTableAdapter accountAdapter = new AccountTableAdapter();
            UserInfoTableAdapter userInfoAdapter = new UserInfoTableAdapter();

            DataTable userInfoDT = userInfoAdapter.GetDataByUsername(username);
            DataRow userInfoRow = userInfoDT.Rows[0];

            int amountOfMoney = userInfoRow.Field<int>("AmountOfMoney");
            DateTime lastUpdatedMoney = userInfoRow.Field<DateTime>("LastUpdatedMoney");
            byte[] fingerPrintIMG = userInfoRow.Field<byte[]>("FingerPrintIMG");
            DateTime? lastUpdatedFingerPrint = userInfoRow.Field<DateTime?>("LastUpdatedFingerPrint");
            int? fingerPosition = userInfoRow.Field<int?>("FingerPosition");
            DateTime insertedDate = userInfoRow.Field<DateTime>("InsertedDate");

            accountAdapter.Connection.Open();
            userInfoAdapter.Connection = accountAdapter.Connection;

            using (SqlTransaction transaction = accountAdapter.Connection.BeginTransaction())
            {
                accountAdapter.AttachTransaction(transaction);
                userInfoAdapter.AttachTransaction(transaction);

                try
                {
                    userInfoAdapter.UpdateUserInfo(username, name, userTypeID, amountOfMoney, lastUpdatedMoney, fingerPrintIMG
                        , lastUpdatedFingerPrint, fingerPosition, isCafeteriaStaff, isActive, insertedDate, updateBy, date, username);
                    Log.ActivityLog("Update to UserInfo: username = "******"Update to Account: username = "******"editUser"] = "******";
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Log.ErrorLog(ex.Message);
                    Session["editUser"] = "******";
                }
            }

            return RedirectToAction("EditUser", "Account", new { @username = model.Username });
        }