Esempio n. 1
0
        public async static Task <CUser> Retrieve(Int32 id)  // overload #1
        {
            CUser ret = null;

            try
            {
                String       sql = "SELECT * FROM `user` WHERE `id` = @id";
                MySqlCommand cmd = new MySqlCommand(sql, Program.conn);
                cmd.Parameters.AddWithValue("@id", id);
                DbDataReader reader = await cmd.ExecuteReaderAsync();

                cmd.Dispose();
                if (!(await reader.ReadAsync()))
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    CUtils.LastLogMsg = "User with id '" + id + "' not found!";
                    return(ret);
                }
                ret = new CUser(id,
                                reader.GetString(reader.GetOrdinal("username")),
                                reader.GetString(reader.GetOrdinal("password")),
                                reader.GetString(reader.GetOrdinal("email")),
                                reader.GetByte(reader.GetOrdinal("type")),
                                (await reader.IsDBNullAsync(reader.GetOrdinal("status"))) ? (Byte)0 : reader.GetByte(reader.GetOrdinal("status")));

                if (!reader.IsClosed)
                {
                    reader.Close();
                }
                CUtils.LastLogMsg = null;
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message + " " + ex.StackTrace);
#endif
                CUtils.LastLogMsg = "Unahandled Exception!";
            }
            return(ret);
        }
Esempio n. 2
0
        private async void button_Submit_Click(object sender, EventArgs e)
        {
            // Get & Validate Username
            String username = textBox_Username.Text.Trim();

            if (!CUtils.IsValidUsername(username))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Username!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Get & Validate Password
            String password = textBox_Password.Text.Trim();

            if (!CUtils.IsValidPassword(password))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Password!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }
            password = CUtils.sha256(password);

            // Get & Validate Account/User Type
            Byte type = (Byte)comboBox_Account.SelectedIndex;

            if (!(await CUtils.IsValidUserType(type)))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Account Type!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Get & Validate EmailID
            String email = textBox_EmailID.Text.Trim();

            if (!CUtils.IsValidEmail(email))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Email!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Register User
            if (!(await CUser.Register(username, password, email, type)))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Unknown", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }
            MessageBox.Show("You have Successfully Registered!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            prev.StartPosition = this.StartPosition;
            prev.Location      = this.Location;
            prev.Visible       = true;
            this.Close();
            this.Dispose();
        }
Esempio n. 3
0
        private async void button_Submit_Click(object sender, EventArgs e)
        {
            // Get & Validate Username
            String username = textBox_Username.Text.Trim();

            if (!CUtils.IsValidUsername(username))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Username!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Get & Validate Password
            String password = textBox_Password.Text.Trim();

            if (!CUtils.IsValidPassword(password))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Password!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }
            password = CUtils.sha256(password);

            // Get & Validate Account/User Type
            Byte type = (Byte)comboBox_Account.SelectedIndex;

            if (!(await CUtils.IsValidUserType(type)))
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Invalid Account Type!", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            // Login User
            CUser.cur_user = await CUser.Retrieve(username, password, type);

            if (CUser.cur_user == null)
            {
                if (CUtils.LastLogMsg != null)
                {
                    MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Cause: Unknown", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return;
            }

            /*if (!(await CUser.cur_user.login()))
             * {
             *  if (CUtils.LastLogMsg != null)
             *      MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
             *  else
             *      MessageBox.Show("Cause: " + CUtils.LastLogMsg, "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             *  return;
             * }*/
            MessageBox.Show("You have Successfully Logged In!" + type, "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            switch (type)
            {
            case 0: new Customer_Home(this).Visible = true; break;

            case 1: new Seller_Home(this).Visible = true; break;
                //case 2: new OrderManager_Home(this).Visible = true; break;
                //case 3: new ProductManager_Home(this).Visible = true; break;
            }
            this.Visible = false;
        }