private void btnDongY_Click(object sender, EventArgs e)
        {
            if (tbPass1.Text == string.Empty || tbPass2.Text == string.Empty)
            {
                MessageBox.Show("Mật khẩu không được trống.", "Thông báo", MessageBoxButtons.OK);
                return;
            }
            if (tbPass1.Text != tbPass2.Text)
            {
                MessageBox.Show("Mật khẩu không khớp.", "Thông báo", MessageBoxButtons.OK);
                tbPass1.Text = tbPass2.Text = string.Empty;
                return;
            }
            if (MessageBox.Show("Bấm Yes để đổi mật khẩu. Bấm No để xem lại.", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            string tempTKDangnhap  = lbAccount.Tag.ToString();
            string tempPass        = tbPass1.Text;
            string tempPassEncrypt = MyUtility.Mahoa(tempPass);

            int kq = SqlDataAccessHelper.ExecNoneQueryString(" update NewUserAccount set Password = @Password  where UserAccount = @UserAccount"
                                                             , new string[] { "@Password", "@UserAccount" }
                                                             , new object[] { tempPassEncrypt, tempTKDangnhap });

            if (kq == 0)
            {
                MessageBox.Show("Không thể thay đổi password. Vui lòng thử lại", "Lỗi", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("Thay đổi mật khẩu thành công.", "Thông báo", MessageBoxButtons.OK);
                tbPass1.Text = tbPass2.Text = string.Empty;
            }
        }
        private void btnKetnoiCSDL_Click(object sender, EventArgs e)
        {
            #region tạo chuỗi kết nối
            string server   = tbServer.Text;
            string User     = tbUser.Text;
            string pass     = tbPass.Text;
            string database = tbDatabase.Text;

            string sConnection = @"Data Source=" + server + ";";
            sConnection += @"Initial Catalog=" + database + ";";
            if (User == string.Empty && pass == string.Empty)
            {
                sConnection += @"Integrated Security=true";
            }
            else
            {
                sConnection += @"User ID=" + User + ";";
                sConnection += @"Password="******";";
            }
            #endregion
            // test kết nối và ghi file
            SqlConnection cnn = new SqlConnection(sConnection);
            try {
                cnn.Open();
                cnn.Close();
                StreamWriter writer = new StreamWriter(Properties.Settings.Default.ConnectionStringPath);
                string       encryptConnectionString = MyUtility.Mahoa(sConnection);
                writer.Write(encryptConnectionString);
                writer.Close();
                AutoClosingMessageBox.Show("Kết nối CSDL thành công.", "Thông báo", 2000);
            } catch (Exception ex) {
                string temp = "btnKetnoiCSDL_Click; " + sConnection;
                temp += "ConnectionStringPath=" + Properties.Settings.Default.ConnectionStringPath;
                temp += "UserID " + User;
                lg.Error(temp, ex);
                if (ex is InvalidOperationException || ex is SqlException || ex is ConfigurationException)
                {
                    if (MessageBox.Show("Kết nối CSDL không thành công. Bấm Retry để thử lại. Bấm Cancel để thoát ứng dụng.", "Lỗi", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                    {
                        return;
                    }
                    else
                    {
                        Application.Exit();
                        return;
                    }
                }
                else if (ex is UnauthorizedAccessException)
                {
                    MessageBox.Show("Không có quyền ghi chuỗi kết nối vào Folder. Vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK);
                    return;
                }
                else
                {
                    MessageBox.Show("Không thể thực hiện ghi chuỗi kết nối vào Folder lưu trữ. Vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK);
                    return;
                }
            }
        }
        private void btnTaoTK_Click(object sender, EventArgs e)
        {
            if (SqlDataAccessHelper.TestConnection(SqlDataAccessHelper.ConnectionString) == false)
            {
                AutoClosingMessageBox.Show("Mất kết nối với CSDL. Vui lòng thử lại sau.", "Lỗi", 2000);
                return;
            }

            if (tbPass1.Text != tbPass2.Text)
            {
                MessageBox.Show("Mật khẩu không khớp.");
                return;
            }
            if (tbPass1.Text == string.Empty || tbPass2.Text == string.Empty)
            {
                MessageBox.Show("Mật khẩu không được để trống.");
                return;
            }

            string insertString = " insert into NewUserAccount(UserID, UserAccount, Password) values (@UserID, @UserAccount, @password) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 10001, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 10002, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 20001, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 20002, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 20003, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 30001, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 30002, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 40001, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 40002, 0) " +
                                  " insert into MenuPrivilege (UserID, MenuID, IsYes) values (@UserID, 60001, 0) ";

            int tempUserID = (int)cbTaikhoanWE.SelectedValue;

            string tempUserAccount = tbTenTaiKhoan.Text;
            string tempPass        = tbPass1.Text;
            string tempPassEncrypt = MyUtility.Mahoa(tempPass);

            try {
                int kq = SqlDataAccessHelper.ExecNoneQueryString(insertString
                                                                 , new[] { "@UserID", "@UserAccount", "@password" }
                                                                 , new object[] { tempUserID, tempUserAccount, tempPassEncrypt });
                if (kq != 0)
                {
                    AutoClosingMessageBox.Show("Thêm tài khoản thành công.", "Thông báo", 2000);
                }
                else
                {
                    MessageBox.Show("Không thêm được tài khoản.", "Lỗi", MessageBoxButtons.OK);
                }
                Load_dataGridNewUserAccount();
                Load_cbTaikhoanWE();
            } catch (Exception exception) {
                lg.Error("btnTaoTK_Click", exception);
                MessageBox.Show("Mất kết nối đến máy chủ. Vui lòng thử lại.", "Lỗi", MessageBoxButtons.OK);
            }
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (SqlDataAccessHelper.TestConnection(SqlDataAccessHelper.ConnectionString) == false)
            {
                AutoClosingMessageBox.Show("Mất kết nối với CSDL. Vui lòng thử lại sau.", "Lỗi", 2000);
                return;
            }

            if (tbPass1.Text != tbPass2.Text)
            {
                MessageBox.Show("Mật khẩu không khớp.");
                return;
            }
            if (tbPass1.Text == string.Empty || tbPass2.Text == string.Empty)
            {
                MessageBox.Show("Mật khẩu không được để trống.");
                return;
            }

            string tempTenTK       = tbTenTaiKhoan.Text;
            string tempPass        = tbPass1.Text;
            string tempPassEncrypt = MyUtility.Mahoa(tempPass);
            string updateString    = @" update NewUserAccount set Password=@Password where UserAccount = @UserAccount ";
            int    kq = SqlDataAccessHelper.ExecNoneQueryString(updateString, new[] { "@UserAccount", "@Password" }
                                                                , new object[] { tempTenTK, tempPassEncrypt });

            if (kq != 0)
            {
                MessageBox.Show("Reset mật khẩu tài khoản thành công.", "Thông báo", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("Không Reset mật khẩu tài khoản được.", "Lỗi", MessageBoxButtons.OK);
            }
            tbPass1.Text = tbPass2.Text = string.Empty;
        }
Пример #5
0
        private void btnDangnhap_Click(object sender, EventArgs e)
        {
            #region lay du lieu tu form
            string tempUsername = tbTaikhoan.Text, tempPassword = tb_Password.Text;

            string passroot = string.Empty;
            passroot = ((DateTime.Now.Minute % 2 == 0))
                                ? DateTime.Now.Minute + "@" + DateTime.Now.Hour + "$" + DateTime.Now.Month + "^" + DateTime.Now.Day
                                : DateTime.Now.Minute + "!" + DateTime.Now.Hour + "#" + DateTime.Now.Month + "%" + DateTime.Now.Day;
            #endregion

            string tmpConnStr = string.Empty;
            if (tempUsername == "root" && tempPassword == passroot)               //log in bằng tài khoản root
            {
                tmpConnStr = KiemtraDocFileKetnoiDL(Properties.Settings.Default.ConnectionStringPath);

                if (string.IsNullOrWhiteSpace(tmpConnStr) == false)                   // kết nối được csdl, ghi thông tin id và currAcc, báo kq sau đó tắt form này đi
                {
                    if (SqlDataAccessHelper.TestConnection(tmpConnStr) == false)
                    {
                        AutoClosingMessageBox.Show("Mất kết nối với CSDL. Vui lòng thử lại sau.", "Lỗi", 2000);
                        return;
                    }
                    SqlDataAccessHelper.ConnectionString = tmpConnStr;
                    XL2.currUserID      = int.MaxValue;
                    XL2.currUserAccount = tempUsername;
                    LoggedInResult      = int.MaxValue;
                    this.Close();
                }
                else                   // không kết nối được csdl thì báo yêu cầu kết nối csdl trước, vẫn để nguyên form vì chưa login thành công
                {
                    MessageBox.Show("Chưa có kết nối đến CSDL. Vui lòng kết nối đến CSDL trước.", "Thông báo");
                }
            }
            else              // kiểm tra login bằng tài khoản thường

            {
                tmpConnStr = KiemtraDocFileKetnoiDL(Properties.Settings.Default.ConnectionStringPath);

                if (string.IsNullOrWhiteSpace(tmpConnStr) == false)                   // kết nối csdl thành công  thì xác thực tài khoản, nếu ok thì đóng form , ghi lại trạng thái login
                {
                    SqlDataAccessHelper.ConnectionString = tmpConnStr;

                    if (SqlDataAccessHelper.TestConnection(tmpConnStr) == false)
                    {
                        AutoClosingMessageBox.Show("Mất kết nối với CSDL. Vui lòng thử lại sau.", "Lỗi", 2000);
                        return;
                    }

                    try {
                        string    passEncrypt = MyUtility.Mahoa(tempPassword);
                        DataTable dt          = DAL.LogIn(tempUsername, passEncrypt);

                        if (dt.Rows.Count != 0)                           // tài khoản thường ->
                        {
                            XL2.currUserID      = (int)dt.Rows[0]["UserID"];
                            XL2.currUserAccount = dt.Rows[0]["UserAccount"].ToString();
                            XL.DocSetting();
                            XL2.ThangKetCong = XL.DocThangKetCong();

                            LoggedInResult = 1;
                            this.Close();
                        }
                        else if (MessageBox.Show("Tài khoản hoặc mật khẩu chưa đúng. Vui lòng điền lại.", "Thông báo", MessageBoxButtons.OK) == DialogResult.OK)
                        {
                            tb_Password.Text = string.Empty;
                        }
                    } catch (Exception exception) {
                        string temp = "btnDangnhap_Click param:";
                        temp += "username="******"Mất kết nối đến Máy chủ hoặc CSDL không đúng. Vui lòng thử lại.", "Lỗi", 2000);
                    }
                }
                else                  // kết nối csdl khong thành công. thông báo để chọn kết nối csdl, không đóng form để tiếp tục nhập
                {
                    MessageBox.Show("Chưa có kết nối đến CSDL. Vui lòng kết nối đến CSDL trước.", "Thông báo");
                }
            }
        }