Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
10
Reply
how to check if user is in database already ?
Tangara G
Jul 28 2016 7:05 AM
385
Reply
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())
{
//try
{
// var checkuser = from u in cntx.Users where u.userName == txtUserName.Text select u;
// var checkuser = from u in cntx.Users where txtUserName.Text == u.userName select u;
var checkuser = cntx.Users.FirstOrDefault(u => txtUserName.Text == u.userName);
//int a = Convert.ToInt32(txtUserId.Text);
// var User = cntx.Users.FirstOrDefault(m => m.userID == a);
// customer.userName = txtUserName.Text;
// String checkUser = "select Count(*) from User where userName = '" + customer.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"
);
}
}
// Response.Redirect("SuperPhoneOnLineShop_Team6/ShoppingPage");// enter the right url here
}
}
}
}
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.
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
10
)
Next Recommended Forum
Not Exporting Ajax ControlToolkit Linechart to excel asp.net
I need to full fill default.aspx on desktop (full size)