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); } } } }