Exemplo n.º 1
0
        //Sự kiện: xảy ra khi button Thêm được click
        // thêm tài khoản vào cơ sở dữ liệu
        private void ButtonThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (textTK.Text.Trim() == "" || textMK1.Text.Trim() == "" || textMK2.Text.Trim() == "")
                {
                    MessageBox.Show("Vui lòng nhập đầy dủ thông tin!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (textMK1.Text == textMK2.Text)// nếu mật khẩu nhập 2 lần như nhau
                    {
                        bool trung = false;

                        // Kiểm tra tài khoản mới nhập đã tồn tại chưa
                        foreach (TAIKHOAN newtk in ListTaiKhoan)
                        {
                            if (newtk.TENTK == textTK.Text)
                            {
                                trung = true;
                                break;
                            }
                        }


                        if (!trung)// nếu chưa tồn tại thì thêm vào
                        {
                            TAIKHOAN newTK = new TAIKHOAN();
                            List <usp_SelectLastMaTKResult> results = _TaiKhoan.LayMaTKCuoiCung();// lấy mã tài khoản cuối cùng

                            foreach (usp_SelectLastMaTKResult result in results)
                            {
                                newTK.MATK = result.MATK + 1;
                            }

                            newTK.TENTK   = textTK.Text;
                            newTK.MATKHAU = MaHoaMD5(MaHoaMD5(textMK1.Text));
                            newTK.LOAITK  = radioGroup1.SelectedIndex;

                            //Add List HocSinh Insert To Save DB
                            ListTaiKhoanInsert.Add(newTK);
                            ListTaiKhoan.Add(newTK);
                            dataPhanQuyen.DataSource = ListTaiKhoan.ToArray();
                            MessageBox.Show("Tài khoản bạn tạo thành công!", "Thêm tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            _luu = true;
                            ClearText();

                            // Coder: Tài
                            Luu();
                        }
                        else
                        {
                            MessageBox.Show("Tài khoản bạn vừa thêm đã tồn tại!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Vui lòng nhập mật khẩu xác nhận giống và mật khẩu giống nhau!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch { }
        }