コード例 #1
0
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            connect = new SqlConnection(connectionString);
            connect.Open();
            String sql = @"SELECT * FROM Staff";

            command    = new SqlCommand(sql, connect);
            dataReader = command.ExecuteReader();

            while (dataReader.Read())
            {
                username = dataReader.GetValue(1).ToString();
                password = dataReader.GetValue(2).ToString();
                userPos  = dataReader.GetValue(3).ToString();

                if (username == tbUsername.Text && password == tbPassword.Text)
                {
                    lblSearching.Text = "";
                    tbUsername.Text   = "";
                    tbPassword.Text   = "";
                    lblSearching.Text = "";

                    if (userPos == "O")
                    {
                        frmOwner owner = new frmOwner();
                        owner.ShowDialog();
                    }
                    else
                    {
                        frmEmployee employee = new frmEmployee();
                        employee.ShowDialog();
                    }
                }

                else
                {
                    if (tbUsername.Text == "" && tbPassword.Text == "")
                    {
                        lblSearching.Text = "Please enter username and password";
                    }
                    else if (tbUsername.Text == "")
                    {
                        lblSearching.Text = "Please enter username";
                    }
                    else if (tbPassword.Text == "")
                    {
                        lblSearching.Text = "Please enter password";
                    }
                    else if (username != tbUsername.Text)
                    {
                        tbUsername.SelectAll();
                        tbPassword.SelectAll();
                        lblSearching.Text = "The username or the password is incorrect";
                    }
                    else if (password != tbPassword.Text)
                    {
                        tbUsername.SelectAll();
                        tbPassword.SelectAll();
                        lblSearching.Text = "The username or the password is incorrect";
                    }
                }
            }

            /* }
             * if (username == tbUsername.Text && password == tbPassword.Text)
             * {
             *   lblSearching.Text = "";
             *   tbUsername.Text = "";
             *   tbPassword.Text = "";
             *
             *   if (userPos == "O")
             *   {
             *       frmOwner owner = new frmOwner();
             *       owner.ShowDialog();
             *       //this.Close();
             *   }
             *   else
             *   {
             *       frmEmployee emp = new frmEmployee();
             *       emp.ShowDialog();
             *      // this.Close();
             *   }
             * }
             * else
             * {
             *   if (username != tbUsername.Text)
             *   {
             *       tbUsername.SelectAll();
             *       tbPassword.SelectAll();
             *       lblSearching.Text = "The username or the password is incorrect";
             *   }
             *   else if (password != tbPassword.Text)
             *   {
             *       tbUsername.SelectAll();
             *       tbPassword.SelectAll();
             *       lblSearching.Text = "The username or the password is incorrect";
             *   }
             * }
             *
             * }*/
            connect.Close();

            /*
             * frmOwner ow = new frmOwner();
             * ow.ShowDialog();
             *
             * frmEmployee emp = new frmEmployee();
             * emp.ShowDialog();*/
        }