Tangara G

Tangara G

  • NA
  • 298
  • 90.2k

how to check if user is in database already ?

Jul 28 2016 7:05 AM
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.
 
  1. protected void Submit_Button_Click(object sender, EventArgs e)  
  2.         {  
  3.             using (Team6_PhoneOnlineShopEntities2 cntx = new Team6_PhoneOnlineShopEntities2())  
  4.             {  
  5.                 //try  
  6.                 {  
  7.                     // var checkuser = from u in cntx.Users where u.userName == txtUserName.Text select u;  
  8.                     // var checkuser = from u in cntx.Users where txtUserName.Text == u.userName select u;  
  9.                      var checkuser = cntx.Users.FirstOrDefault(u => txtUserName.Text == u.userName);  
  10.                     //int a = Convert.ToInt32(txtUserId.Text);  
  11.                     // var User = cntx.Users.FirstOrDefault(m => m.userID == a);  
  12.   
  13.   
  14.                    // customer.userName = txtUserName.Text;  
  15.   
  16.                     // String checkUser = "select Count(*) from User where userName = '" + customer.userName + "'";  
  17.                     
  18.   
  19.                     if (checkuser == null)  
  20.                       
  21.                     {  
  22.                         User customer = new User();  
  23.                         customer.userName = txtUserName.Text;  
  24.                         customer.lastName = txtLastName.Text;  
  25.                         customer.userPassword = txtUserPassword.Text;  
  26.                         customer.userPassword = txtConfirmedPassword.Text;  
  27.                         customer.moblieNumber = txtMobileNo.Text;  
  28.                         customer.userAddress = txtAddress.Text;  
  29.                         customer.userEmail = txtEmail.Text;  
  30.                         customer.roleID = 1;  
  31.                         customer.paymentMethod = DropDownList1.SelectedValue;  
  32.                         cntx.Users.Add(customer);  
  33.                         cntx.SaveChanges();  
  34.                         lblMessageSuccess.Text = "successful registration";  
  35.                     }  
  36.                     else  
  37.                     {  
  38.                         lblMessageUser.Text = "You have registered before. Pls proceed to login";  
  39.                         Response.Redirect("/Login.aspx");  
  40.                     }  
  41.   
  42.                 }  
  43.   
  44.                 // Response.Redirect("SuperPhoneOnLineShop_Team6/ShoppingPage");// enter the right url here  
  45.   
  46.             }  
  47.         }  
  48.     }  

 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.
 
 

Answers (10)