private void btnLogIn_Click(object sender, EventArgs e)
        {
            MarioCraftModel ctx = new MarioCraftModel();
            //gameUser = ctx.GAMEUSERs.First();

            var user = from u in ctx.GAMEUSERs where u.USEREMAIL == txtEmail.Text select u;

            //var gu = from u in ctx.GAMEUSERs where u.USERPASSWORD == txtPassword.Text select u;

            //if either email or password field empty display error message
            if (txtEmail.Text.Equals("") && txtPassword.Text.Equals(""))
            {
                MessageBox.Show("Email and Password field cannot be empty", "Email or Password field empty",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }


            //https://dotprogramming.blogspot.com/2015/09/login-form-using-entity-framework-in.html
            //check if user email matches database
            var userExist = user.FirstOrDefault(a => a.USEREMAIL.Equals(txtEmail.Text));

            //if email match record in database
            if (userExist != null)
            {
                //if password matches records in database
                if (userExist.USERPASSWORD.Equals(txtPassword.Text))
                {
                    MessageBox.Show("Log in success!", "Log In Confirmation",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);


                    formUserAccount gameScreen = new formUserAccount(txtEmail.Text);
                    this.Close();
                    gameScreen.Show();
                }
                else
                {
                    errorProvider1.SetError(txtPassword, "Password does not match record,please try again");
                    txtPassword.Focus();
                    return;
                }
            }
            else
            {
                MessageBox.Show("Game user not found",
                                "Invalid Email address",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }
        }
Esempio n. 2
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtEmail.Text.Equals(""))
            {
                MessageBox.Show("Email field cannot be empty", "Email field empty",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }


            var user       = (from u in ctx.GAMEUSERs where u.USEREMAIL == email select u);
            var emailExist = user.FirstOrDefault(a => a.USEREMAIL.Equals(txtEmail.Text));

            //if amended email already exist in db
            if (emailExist != null)
            {
                if (emailExist.USEREMAIL.Equals(txtEmail.Text))
                {
                    MessageBox.Show("Email address has been used, please enter another one", "Email cannot be used",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtEmail.Focus();
                    return;
                }
            }
            else
            {
                using (var context = new MarioCraftModel())
                {
                    userUpdate.USEREMAIL = txtEmail.Text;

                    try//update new email
                    {
                        context.SaveChanges();
                        MessageBox.Show("Details updated", "Account Details updated",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            MarioCraftModel ctx         = new MarioCraftModel();
            GAMEUSER        addGameUser = new GAMEUSER();

            var user = from u in ctx.GAMEUSERs where u.USEREMAIL == txtEmail.Text select u;

            //check if user email matches database
            var userExist = user.FirstOrDefault(a => a.USEREMAIL.Equals(txtEmail.Text));

            //if either email or password field empty display error message
            if (txtEmail.Text.Equals("") && txtPassword.Text.Equals(""))
            {
                MessageBox.Show("Email and Password field must not be empty", "Email or Password field empty",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }

            //if email matches records in database
            if (userExist != null)
            {
                //if password matches records in database
                if (userExist.USERPASSWORD.Equals(txtPassword.Text))
                {
                    MessageBox.Show("Game User already exist, please log in", "Registeration error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    formMainMenu mainMenu = new formMainMenu(this);
                    this.Close();
                    mainMenu.Show();
                }
            }
            else //insert user
            {
                using (var context = new MarioCraftModel())
                {
                    addGameUser.USEREMAIL    = txtEmail.Text;
                    addGameUser.USERPASSWORD = txtPassword.Text;

                    try
                    {
                        context.GAMEUSERs.Add(addGameUser);
                        context.SaveChanges();
                        MessageBox.Show(addGameUser.USEREMAIL + "\n" + addGameUser.USERPASSWORD + "\n" +
                                        "You have Successfully Registered, Please log in", "Registered", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        formMainMenu mainMenu = new formMainMenu(this);
                        this.Close();
                        mainMenu.Show();

                        ClearForm();
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }