Пример #1
0
 private void buttonCancelNewStaff_Click(object sender, EventArgs e)
 {
     if (administrator != null)
     {
         administrator.Show();
         this.Close();
     }
     else if (staff != null)
     {
         staff.Show();
         this.Close();
     }
     else
     {
         /** Think of a scenario. Think of a form to get open in default case. **/
     }
 }
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            /*if(!(textBoxUserName.Text=="1" && textBoxPassword.Text=="1"))
             * {
             * MessageBox.Show("Invalid username or password.", "Login Failure!");
             * }*/


            if (radioButtonAdministrator.Checked == false && radioButtonStaff.Checked == false && radioButtonClient.Checked == false)
            {
                MessageBox.Show("Please select a user type.", "Login Failure!");
            }
            else
            {
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                SqlDataReader dataReader;

                if (radioButtonAdministrator.Checked == true)
                {
                    sqlCommand.CommandText = "SELECT s.StaffName " +
                                             "FROM Staff s, Administrator a " +
                                             "WHERE a.Staff_StaffID=s.StaffID AND CurrentAdmin=1";
                    sqlCommand.CommandType = CommandType.Text;

                    dataReader = sqlCommand.ExecuteReader();

                    int columnCount = dataReader.FieldCount;

                    string adminName = "";
                    while (dataReader.Read() == true)
                    {
                        if (dataReader.GetValue(0).ToString().Equals(textBoxUserName.Text.ToString()))
                        {
                            adminName = textBoxUserName.Text.ToString();
                            break;
                        }
                    }
                    dataReader.Close();

                    if (adminName != "")
                    {
                        sqlCommand.CommandText = string.Format("SELECT LoginPassword FROM Staff WHERE StaffName like '{0}'", adminName);
                        sqlCommand.CommandType = CommandType.Text;

                        dataReader = sqlCommand.ExecuteReader();

                        if (dataReader.Read() == true)
                        {
                            if (textBoxPassword.Text.ToString().Equals(dataReader.GetValue(0)))
                            {
                                loginDetails.Add(textBoxUserName.Text);
                                loginDetails.Add(textBoxPassword.Text);

                                Administrator administrator = new Administrator(this);
                                administrator.Show();
                                this.Hide();
                            }
                            else
                            {
                                MessageBox.Show("Invalid username or password.", "Login Failure!");
                            }
                        }
                        dataReader.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid username or password.", "Login Failure!");
                    }
                }
                else if (radioButtonStaff.Checked == true)
                {
                    sqlCommand.CommandText = "SELECT StaffName FROM Staff";
                    sqlCommand.CommandType = CommandType.Text;

                    dataReader = sqlCommand.ExecuteReader();

                    int columnCount = dataReader.FieldCount;

                    string staffName = "";
                    while (dataReader.Read() == true)
                    {
                        if (dataReader.GetValue(0).ToString().Equals(textBoxUserName.Text.ToString()))
                        {
                            staffName = textBoxUserName.Text.ToString();
                            break;
                        }
                    }
                    dataReader.Close();

                    if (staffName != "")
                    {
                        sqlCommand.CommandText = string.Format("SELECT LoginPassword FROM Staff WHERE StaffName like '{0}'", staffName);
                        sqlCommand.CommandType = CommandType.Text;

                        dataReader = sqlCommand.ExecuteReader();

                        if (dataReader.Read() == true)
                        {
                            if (textBoxPassword.Text.ToString().Equals(dataReader.GetValue(0)))
                            {
                                loginDetails.Add(textBoxUserName.Text);
                                loginDetails.Add(textBoxPassword.Text);

                                Staff staff = new Staff(this);
                                staff.Show();
                                this.Hide();
                            }
                            else
                            {
                                MessageBox.Show("Invalid username or password.", "Login Failure!");
                            }
                        }
                        dataReader.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid username or password.", "Login Failure!");
                    }
                }
                else if (radioButtonClient.Checked == true)
                {
                    sqlCommand.CommandText = "SELECT UserName FROM Customer";
                    sqlCommand.CommandType = CommandType.Text;

                    dataReader = sqlCommand.ExecuteReader();

                    int columnCount = dataReader.FieldCount;

                    string customerName = "";
                    while (dataReader.Read() == true)
                    {
                        if (dataReader.GetValue(0).ToString().Equals(textBoxUserName.Text.ToString()))
                        {
                            customerName = textBoxUserName.Text.ToString();
                            break;
                        }
                    }
                    dataReader.Close();

                    if (customerName != "")
                    {
                        sqlCommand.CommandText = string.Format("SELECT LoginPassword FROM Customer WHERE UserName like '{0}'", customerName);
                        sqlCommand.CommandType = CommandType.Text;

                        dataReader = sqlCommand.ExecuteReader();

                        if (dataReader.Read() == true)
                        {
                            if (textBoxPassword.Text.ToString().Equals(dataReader.GetValue(0)))
                            {
                                loginDetails.Add(textBoxUserName.Text);
                                loginDetails.Add(textBoxPassword.Text);

                                Client client = new Client(this);
                                client.Show();
                                this.Hide();
                            }
                            else
                            {
                                MessageBox.Show("Invalid username or password.", "Login Failure!");
                            }
                        }
                        dataReader.Close();
                    }
                    else
                    {
                        MessageBox.Show("Invalid username or password.", "Login Failure!");
                    }
                }
                //MessageBox.Show(loginDetails[0]);
            }
        }