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

Bikesh SrivastavaRank #108 · 20.3K reputationPosted Jul 28, 2016, 3:15 AM EDT
Tangara GRank #4,983 · 316 reputationPosted Jul 28, 2016, 5:42 AM EDT
ali tuncerRank #1,499 · 1.1K reputationPosted Jul 28, 2016, 5:44 PM EDT
what if txtUserName.Text is empty? since you don't validate before you save, you might be saving empty values(blanks) to database..check the records in your database..
try this code to check is username exists :
var checkuser = cntx.Users.Where(u => u.userName == txtUserName.Text).FirstOrDefault();
Manas MohapatraRank #182 · 13.2K reputationPosted Jul 28, 2016, 9:06 PM EDT
Tangara GRank #4,983 · 316 reputationPosted Jul 28, 2016, 10:47 PM EDT
ali tuncerRank #1,499 · 1.1K reputationPosted Jul 28, 2016, 11:19 PM EDT
Hi Karen,
Tangara GRank #4,983 · 316 reputationPosted Jul 31, 2016, 6:45 AM EDT
Manas MohapatraRank #182 · 13.2K reputationPosted Jul 31, 2016, 7:08 AM EDT
Imran Bashir120 reputationPosted Jul 31, 2016, 10:50 AM EDT
{
if (Page.IsValid)
{
using (db.con)
{
db.com = new SqlCommand("sp_user_register", db.con);
db.com.CommandType = CommandType.StoredProcedure;
//string khan = DateTime.Now.ToString();
db.com.Parameters.AddWithValue("@user_name",txtUserName.Text.Trim());
db.com.Parameters.AddWithValue("@password",txtPassword.Text.Trim());
db.com.Parameters.AddWithValue("@first_name ",txtFirstName.Text.Trim());
db.com.Parameters.AddWithValue("@last_name",txtLastName.Text.Trim());
db.com.Parameters.AddWithValue("@address",txtAddress.Text.Trim());
db.com.Parameters.AddWithValue("@contact",txtPhone.Text.Trim());
try
{
db.con.Open();
int ReturnCode = (int)db.com.ExecuteScalar();
if (ReturnCode == -1)
{
lblResult.Text = "user name is already in use";
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
Response.AddHeader("REFRESH", "2;URL=regSalesman.aspx");
}
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}
}
else
lblResult.Text = "some Fields are missing";
}
}
Tangara GRank #4,983 · 316 reputationPosted Jul 31, 2016, 3:14 PM EDT