Пример #1
0
        private void buttonLogout_Submit_Click_1(object sender, EventArgs e)
        {
            DB db = new DB();

            String username = textUsername.Text;
            String password = textPassword.Text;

            DataTable table = new DataTable();

            MySqlDataAdapter adapter = new MySqlDataAdapter();

            //Validation of the login input
            MySqlCommand command = new MySqlCommand("SELECT * FROM `user_data` WHERE `username` = @usn and `password` = @pass", db.getConnect());

            command.Parameters.Add("@usn", MySqlDbType.VarChar).Value  = username;
            command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = password;

            adapter.SelectCommand = command;

            adapter.Fill(table);

            if (table.Rows.Count > 0)
            {
                MySqlCommand command2 = new MySqlCommand("SELECT * FROM `login_current` WHERE `username` = @username", db.getConnect());
                command2.Parameters.Add("@username", MySqlDbType.VarChar).Value = username;

                command2.CommandType = CommandType.Text;
                command2.Connection  = Connect;
                Connect.Open();
                try
                {
                    MySqlDataReader dr;
                    dr = command2.ExecuteReader();
                    while (dr.Read())
                    {
                        if ((string)dr["username"] == textUsername.Text)
                        {
                            id         = (int)dr["id"];
                            student_no = (string)dr["student_no"];
                            name       = (string)dr["name"];
                            course     = (string)dr["course"];
                            address    = (string)dr["address"];
                            phone_no   = (int)dr["phone_no"];
                            break;
                        }
                    }
                    dr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                Connect.Close();

                MySqlCommand UpdateRecord = new MySqlCommand("UPDATE `records` SET `Logout_time`= CURRENT_TIMESTAMP WHERE `username` = '" + textUsername.Text + "' ORDER BY `Login_time` DESC LIMIT 1", db.getConnect());

                try
                {
                    db.openConnection();
                    if (UpdateRecord.ExecuteNonQuery() == 1)
                    {
                        MySqlCommand DeleteLogin_current = new MySqlCommand("DELETE FROM `login_current` WHERE `login_current`.`id` = " + id + "", db.getConnect());
                        try
                        {
                            db.openConnection();
                            if (DeleteLogin_current.ExecuteNonQuery() == 1)
                            {
                                MessageBox.Show("Logout Successful");
                                this.Hide();
                                Home Home = new Home();
                                Home.ShowDialog();
                                this.Close();
                            }

                            else
                            {
                                MessageBox.Show("Logout Failed");
                            }
                            db.closeConnection();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    else
                    {
                        MessageBox.Show("Logout Failed");
                    }
                    db.closeConnection();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Please check your username or password", "Invalid Input");
            }
        }