private void btn_login_Click(object sender, EventArgs e)
        {
            if (_Staff.StaffList.Count == 0)
            {
                MessageBox.Show("No account is registered yet.");
                return;
            }

            foreach (Person item in _Staff.StaffList)
            {
                if (txtbx_username.Text == item.Name && txtbx_password.Text == item.Password && (Role)Enum.Parse(typeof(Role), cmbx_roles.SelectedValue.ToString()) == Role.Officant)
                {
                    MessageBox.Show("Login is successful");
                    this.Hide();
                    OfficiantForm officiantForm = new OfficiantForm(this, ProductItemList);
                    officiantForm.Show();
                    break;
                }
                else if (txtbx_username.Text == item.Name && txtbx_password.Text == item.Password && (Role)Enum.Parse(typeof(Role), cmbx_roles.SelectedValue.ToString()) == Role.Manager)
                {
                    MessageBox.Show("Login is successful");
                    this.Hide();
                    ManagerForm managerForm = new ManagerForm(this, ProductItemList, _Staff);
                    managerForm.Show();
                    break;
                }
                else if (_Staff.StaffList.IndexOf(item) == _Staff.StaffList.Count - 1)
                {
                    MessageBox.Show("Login failed");
                }
            }
        }
Beispiel #2
0
        private void btn_login_Click(object sender, EventArgs e)
        {
            string nameInput     = txtbx_username.Text;
            string passwordInput = txtbx_password.Text;
            int    roleIndex     = cmbx_roles.SelectedIndex + 1;

            sqlConnection.Open();
            string        query         = @"SELECT s.Name, s.RoleId FROM Staff  as s 
                            WHERE s.Name='" + nameInput + "' AND Password='******' AND s.RoleId=" + roleIndex;
            SqlCommand    sqlCommand    = new SqlCommand(query, sqlConnection);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (!sqlDataReader.HasRows)
            {
                MessageBox.Show("Login failed");
                txtbx_username.Clear();
                txtbx_password.Clear();
            }

            while (sqlDataReader.Read())
            {
                if (roleIndex == 1)
                {
                    MessageBox.Show("Login is successful");
                    this.Hide();
                    OfficiantForm officiantForm = new OfficiantForm(this, txtbx_username.Text);
                    officiantForm.Show();
                }
                else if (roleIndex == 2)
                {
                    MessageBox.Show("Login is successful");
                    this.Hide();
                    ManagerForm managerForm = new ManagerForm(this);
                    managerForm.Show();
                }
                else
                {
                    MessageBox.Show("Login is successful");
                }
            }
            sqlConnection.Close();
            sqlCommand.Dispose();
            sqlDataReader.Close();
        }