public void loadUserInfo(User user)
 {
     User u = user;
     tbFullName.Text = u.fullname;
     tbAddress.Text = u.address;
     tbBirthdate.Text = u.birthdate;
     tbEmail.Text = u.email;
     tbContact.Text = u.contact;
     lblCurrentBalance.Text = Convert.ToDouble(u.balance).ToString();
     lblTotalExpense.Text = Convert.ToDouble(u.totalExpense).ToString();
 }
 public AdminUserProfileForm(UsersForm uf, User u)
 {
     adminView = true;
     this.uf = uf;
     this.u = u;
     InitializeComponent();
     gbChangePassword.Visible = false;
     gbBalance.Location = new Point(12, 270);
     this.Size = new Size(this.Width, 470);
     loadUserInfo(u);
     disableControls();
 }
 private void btnAdd_Click(object sender, EventArgs e)
 {
     if (tbUsername.Text.Trim() == "" || tbPassword.Text == "" || tbConfirmPassword.Text == "")
     {
         MessageBox.Show("Please enter a username and password correctly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     else
     {
         if (tbPassword.Text == tbConfirmPassword.Text)
         {
             if (admin)
             {
                 Admin a = new Admin(0, tbUsername.Text, tbPassword.Text, tbFullName.Text,
                     tbAddress.Text, tbBirthdate.Text, tbEmail.Text, tbContact.Text);
                 try
                 {
                     Result r = DataServices.SaveAdmin(a, false);
                     if (r.success == true)
                     {
                         MessageBox.Show(r.message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         uf.updateUsers();
                         this.Dispose();
                     }
                     else
                     {
                         MessageBox.Show(r.message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     }
                 }
                 catch (Exception x)
                 {
                     MessageBox.Show("Error in adding admin: " + x.Message, "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
             else
             {
                 User u = new User(0, tbUsername.Text, tbPassword.Text, tbFullName.Text,
             tbAddress.Text, tbBirthdate.Text, tbEmail.Text, tbContact.Text, DateTime.Today, 0, 0);
                 try
                 {
                     Result r = DataServices.SaveUser(u, false);
                     if (r.success == true)
                     {
                         MessageBox.Show(r.message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         uf.updateUsers();
                         this.Dispose();
                     }
                     else
                     {
                         MessageBox.Show(r.message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     }
                 }
                 catch (Exception x)
                 {
                     MessageBox.Show("Error in adding user: "******"Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
         }
         else
         {
             MessageBox.Show("Password did match!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         }
     }
 }
 internal static List<User> searchUsers(string key)
 {
     List<User> list = new List<User>();
     try
     {
         cmd = new OleDbCommand("SELECT * FROM Users WHERE Username LIKE '%" + key + "%';", conn);
         reader = cmd.ExecuteReader();
         while (reader.Read())
         {
             User u = new User(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4),
                reader.GetString(5), reader.GetString(6), reader.GetString(7), reader.GetDateTime(8), reader.GetInt32(9), reader.GetInt32(10));
             list.Add(u);
         }
         return list;
     }
     catch (Exception e)
     {
         MessageBox.Show("Error getting usersList in searchUsers: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return null;
     }
 }
        internal static Result SaveUser(User u, bool update)
        {
            Result r = new Result();

            if (update)
            {
                cmd = new OleDbCommand("UPDATE Users SET Fullname = ?, Address = ?, Birthdate = ?, Email = ?, Contact = ? WHERE ID = ?;", conn);
                try
                {
                    cmd.Parameters.Add("Fullname", OleDbType.VarChar).Value = u.fullname;
                    cmd.Parameters.Add("Address", OleDbType.VarChar).Value = u.address;
                    cmd.Parameters.Add("Birthdate", OleDbType.VarChar).Value = u.birthdate;
                    cmd.Parameters.Add("Email", OleDbType.VarChar).Value = u.email;
                    cmd.Parameters.Add("Contact", OleDbType.VarChar).Value = u.contact;
                    cmd.Parameters.Add("ID", OleDbType.Integer).Value = u.id;

                    cmd.ExecuteNonQuery();
                    r.success = true;
                    r.message = "Updating User Information Successful!";
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error Occured: " + e.Message + " : " + e.Source, "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    r.success = false;
                    r.message = "Updating User Information Failed!";
                }
            }
            else
            {
                List<User> usersList = GetUsers();
                foreach (User user in usersList)
                {
                    if (user.username == u.username)
                    {
                        r.success = false;
                        r.message = "Username is already taken.";
                        return r;
                    }
                }

                cmd = new OleDbCommand("INSERT INTO Users (Username, [Password], Fullname, Address, Birthdate, Email, Contact, DateRegistered) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", conn);

                try
                {
                    cmd.Parameters.Add("Username", OleDbType.VarChar).Value = u.username;
                    cmd.Parameters.Add("Password", OleDbType.VarChar).Value = u.password;
                    cmd.Parameters.Add("Fullname", OleDbType.VarChar).Value = u.fullname;
                    cmd.Parameters.Add("Address", OleDbType.VarChar).Value = u.address;
                    cmd.Parameters.Add("Birthdate", OleDbType.VarChar).Value = u.birthdate;
                    cmd.Parameters.Add("Email", OleDbType.VarChar).Value = u.email;
                    cmd.Parameters.Add("Contact", OleDbType.VarChar).Value = u.contact;
                    cmd.Parameters.Add("DateRegistered", OleDbType.Date).Value = u.dateregistered;

                    cmd.ExecuteNonQuery();
                    r.success = true;
                    r.message = "Adding User Successful!";
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error Occured: " + e.Message + " : " + e.Source, "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    r.success = false;
                    r.message = "Adding User Failed!";
                }
            }
            return r;
        }
 internal static List<User> GetUsers()
 {
     List<User> usersList = new List<User>();
     try
     {
         cmd = new OleDbCommand("SELECT * FROM Users ORDER BY ID;", conn);
         reader = cmd.ExecuteReader();
         while (reader.Read())
         {
             User u = new User(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4),
                 reader.GetString(5), reader.GetString(6), reader.GetString(7), reader.GetDateTime(8), reader.GetInt32(9), reader.GetInt32(10));
             usersList.Add(u);
         }
         reader.Close();
         return usersList;
     }
     catch (Exception e)
     {
         MessageBox.Show("Error getting usersList: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return null;
     }
 }
        internal static Result addBalance(User u, double balance)
        {
            Result r = new Result();
            double newBalance = u.balance + balance;
            double newTotalExpense = u.totalExpense + balance;
            try
            {
                cmd = new OleDbCommand("UPDATE Users SET CurrentBalance = ?, TotalExpense = ? WHERE Username = ?", conn);
                cmd.Parameters.Add("CurrentBalance", OleDbType.Integer).Value = newBalance;
                cmd.Parameters.Add("TotalExpense", OleDbType.Integer).Value = newTotalExpense;
                cmd.Parameters.Add("Username", OleDbType.VarChar).Value = u.username;

                cmd.ExecuteNonQuery();
                r.success = true;
                r.message = "Adding balance successful!";
            }
            catch (Exception e)
            {
                r.success = false;
                r.message = "Adding balance failed! " + e.Message;
            }
            return r;
        }
        public Result SaveUser(User u)
        {
            Result r = new Result();

            cmd = new OleDbCommand("UPDATE Users SET Fullname = ?, Address = ?, Birthdate = ?, Email = ?, Contact = ? WHERE ID = ?;", conn);
            try
            {
                cmd.Parameters.Add("Fullname", OleDbType.VarChar).Value = u.fullname;
                cmd.Parameters.Add("Address", OleDbType.VarChar).Value = u.address;
                cmd.Parameters.Add("Birthdate", OleDbType.VarChar).Value = u.birthdate;
                cmd.Parameters.Add("Email", OleDbType.VarChar).Value = u.email;
                cmd.Parameters.Add("Contact", OleDbType.VarChar).Value = u.contact;
                cmd.Parameters.Add("ID", OleDbType.Integer).Value = u.id;

                cmd.ExecuteNonQuery();
                r.success = true;
                r.message = "Updating User Information Successful!";
            }
            catch (Exception e)
            {
                MessageBox.Show("Error Occured: " + e.Message + " : " + e.Source, "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                r.success = false;
                r.message = "Updating User Information Failed!";
            }
            return r;
        }
 public Result getUserInfo(string username)
 {
     Result r = new Result();
     try
     {
         cmd = new OleDbCommand("SELECT * FROM Users WHERE Username = ?;", conn);
         cmd.Parameters.Add("Username", OleDbType.VarChar).Value = username;
         reader = cmd.ExecuteReader();
         if (reader.Read())
         {
             User u = new User(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4),
                 reader.GetString(5), reader.GetString(6), reader.GetString(7), reader.GetDateTime(8), reader.GetInt32(9), reader.GetInt32(10));
             r.user = u;
             r.success = true;
         }
         return r;
     }
     catch (Exception e)
     {
         MessageBox.Show("Error getting userInfo: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return null;
     }
 }
 public void updateInfo()
 {
     List<User> usersList = DataServices.GetUsers();
     foreach (User user in usersList)
     {
         if (u.username == user.username)
         {
             this.u = user;
             loadUserInfo(u);
         }
     }
 }