private async void btnLogin_Click(object sender, EventArgs e) { if (txtUsername.Text == "" || txtPassword.Text == "") { MessageBox.Show("Username and password must not be null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (lstAdminAccount == null) { AdminAccountWrapper adminAccountWrapper = new AdminAccountWrapper(); lstAdminAccount = await adminAccountWrapper.List(); } string username = txtUsername.Text; string password = ARSUtilities.Md5Hash(txtPassword.Text); AdminAccount adminAccount = lstAdminAccount.Where(aa => aa.Username == username && aa.Password.ToLower() == password.ToLower()).SingleOrDefault(); if (adminAccount == null) { MessageBox.Show("Wrong username or password!", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } fMain.isLoggedIn = isLoggedIn = true; Close(); }
private async void btnDelete_Click(object sender, EventArgs e) { // Lay tai khoan admin dang duoc chon trong bang AdminAccount adminAccount = GetSelectedAdminAccount(); // Neu hien tai khong co tai khoan nao duoc chon thi hien thong bao if (adminAccount == null) { MessageBox.Show("You must choose an account to edit!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // Neu co tai khoan dang duoc chon thi sua cot IsActive lai thanh False adminAccount.IsActive = false; // Gui len server de cap nhat lai cot IsActive trong CSDL AdminAccountWrapper adminAccountWrapper = new AdminAccountWrapper(); bool isSuccess = await adminAccountWrapper.Put(adminAccount.ID, adminAccount); // Kiem tra ket qua tra ve if (isSuccess) { // Neu ket qua la thanh cong, hien thong bao thanh cong MessageBox.Show("Account status was set to inactive!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); // Load lai bang LoadDataGridView(); } else { // Neu ket qua that bai, hien thong bao loi MessageBox.Show("An error has occurred!\n" + adminAccountWrapper.GetErrorMessage(), "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void btnSubmit_Click(object sender, EventArgs e) { // Lay gia tri tren form gan vao account adminAccount.Username = txtUsername.Text; adminAccount.Name = txtName.Text; adminAccount.Role = txtRole.Text; adminAccount.IsActive = rbtnActive.Checked; // Neu dang o che do tao account moi thi phai lay gia tri trong o password gan vao account. // Neu dang o che do chinh sua ma gia tri trong o password khac voi gia tri password cua account // nghia la nguoi dung da thay doi password, phai cap nhat luon password if (mode == FormMode.CREATE || txtPassword.Text != adminAccount.Password) { adminAccount.Password = ARSUtilities.Md5Hash(txtPassword.Text); } // Tao mot API AdminAccountWrapper adminAccountWrapper = new AdminAccountWrapper(); // Tao bien luu ket qua tra ve bool isSuccess; // Kiem tra xem dang o che do nao if (mode == FormMode.CREATE) { // Neu dang o che do them moi (CREATE) // POST account len server isSuccess = await adminAccountWrapper.Post(adminAccount); } else { // Neu dang o che do chinh sua (EDIT) // PUT account len server isSuccess = await adminAccountWrapper.Put(adminAccount.ID, adminAccount); } // Kiem tra ket qua tra ve if (isSuccess) { // Neu thanh cong MessageBox.Show("Operation completed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); // Tat form CE this.Close(); } else { // Neu that bai, hien thong bao loi MessageBox.Show("An error has occurred:\n" + adminAccountWrapper.GetErrorMessage(), "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public async void LoadDataGridView() { // Luu lai dong hien tai dang chon int currentRowIndex; // Neu hien tai khong co dong nao duoc chon thi mac dinh la dong so 0 if (dgvAdmin.SelectedRows.Count == 0) { currentRowIndex = 0; } else { currentRowIndex = dgvAdmin.Rows.IndexOf(dgvAdmin.SelectedRows[0]); } // Luu lai dong hien tai dang o dau` bang? trong dataGridView int firstRowIndex = dgvAdmin.FirstDisplayedScrollingRowIndex; if (firstRowIndex == -1) { firstRowIndex = 0; } // Goi API lay du lieu ve AdminAccountWrapper adminAccountWrapper = new AdminAccountWrapper(); List <AdminAccount> lstAdminAccount = await adminAccountWrapper.List(); // Tao bang de chua du lieu tra ve tu API DataTable table = new DataTable(); table.Columns.Add("ID"); table.Columns.Add("Username"); table.Columns.Add("Password"); table.Columns.Add("Name"); table.Columns.Add("Role"); table.Columns.Add("IsActive"); // Kiem tra xem ket qua goi API co thanh cong hay khong if (lstAdminAccount != null) { // Lap qua tung "dong` du lieu" foreach (AdminAccount adminAccount in lstAdminAccount) { // Tao mot dong` trong bang winForm DataRow row = table.NewRow(); // Gan du lieu len dong moi tao row["ID"] = adminAccount.ID; row["Username"] = adminAccount.Username; row["Password"] = adminAccount.Password; row["Name"] = adminAccount.Name; row["Role"] = adminAccount.Role; row["IsActive"] = adminAccount.IsActive; // Gan dong moi tao vao bang table.Rows.Add(row); } // Sau khi lap qua het cac dong du lieu // Ta co bang WinForm hoan chinh // Gan bang WinForm len DataGridView dgvAdmin.DataSource = table; // An cot password dgvAdmin.Columns["Username"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dgvAdmin.Columns["Password"].Visible = false; // Chon lai dong ban dau duoc chon truoc khi reload if (dgvAdmin.Rows.Count > 0) { dgvAdmin.Rows[currentRowIndex].Selected = true; } // Cuon. toi' dong` duoc. chon. if (dgvAdmin.Rows.Count > 0) { dgvAdmin.FirstDisplayedScrollingRowIndex = firstRowIndex; } } }