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
- }
- }
- }
- }
Tangara GPosted Jul 31, 2016, 7:14 PM
Imran BashirPosted Jul 31, 2016, 2:50 PM
{
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";
}
}
Manas MohapatraPosted Jul 31, 2016, 11:08 AM
Tangara GPosted Jul 31, 2016, 10:45 AM
ali tuncerPosted Jul 29, 2016, 3:19 AM
Hi Karen,
Tangara GPosted Jul 29, 2016, 2:47 AM
Manas MohapatraPosted Jul 29, 2016, 1:06 AM
ali tuncerPosted Jul 28, 2016, 9:44 PM
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();
Tangara GPosted Jul 28, 2016, 9:42 AM
Bikesh SrivastavaPosted Jul 28, 2016, 7:15 AM