I have a form which requires new user to register. If they are already in the database, then they will be re-direct to the login page. If not, they can proceed with the user registration.
However, I am not sure what is wrong with my code cos even if the username is not found, it still direct them to the login page.
- protected void Submit_Button_Click(object sender, EventArgs e)
- {
- using (Team6_PhoneOnlineShopEntities2 cntx = new Team6_PhoneOnlineShopEntities2())
- {
-
- {
-
-
- var checkuser = cntx.Users.FirstOrDefault(u => txtUserName.Text == u.userName);
-
-
-
-
-
-
-
-
-
- if (checkuser == null)
-
- {
- User customer = new User();
- customer.userName = txtUserName.Text;
- customer.lastName = txtLastName.Text;
- customer.userPassword = txtUserPassword.Text;
- customer.userPassword = txtConfirmedPassword.Text;
- customer.moblieNumber = txtMobileNo.Text;
- customer.userAddress = txtAddress.Text;
- customer.userEmail = txtEmail.Text;
- customer.roleID = 1;
- customer.paymentMethod = DropDownList1.SelectedValue;
- cntx.Users.Add(customer);
- cntx.SaveChanges();
- lblMessageSuccess.Text = "successful registration";
- }
- else
- {
- lblMessageUser.Text = "You have registered before. Pls proceed to login";
- Response.Redirect("/Login.aspx");
- }
-
- }
-
-
-
- }
- }
- }
- }
I hope someone can point out what is the way to do my code so that it will turn out the way I want. Many thanks.