This code below is for user sign up i also have the code that checks if the usernameis available if not show the error message user not available and prevent the user fromsubmitting data. So the issue is that the error message is showing if the username is not available but the form still submitesinstead of preventing the submittion.
string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Insert_Users"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
//Get Filename from fileupload control
// string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save images into Images folder
// FileUpload1.SaveAs(Server.MapPath("UserImages/" + filename));
cmd.CommandType = CommandType.StoredProcedure;
// cmd1.Parameters.AddWithValue("@UserImageName", filename);
// cmd1.Parameters.AddWithValue("@UserImagePath", "UserImages/" + filename);
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Parameters.AddWithValue("@UserName", UsrNm);
cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text.Trim()));
cmd.Parameters.AddWithValue("@BirthDay", txtBirthDay.Text.Trim());
cmd.Parameters.AddWithValue("@Country", ddlCountry.Text.Trim());
cmd.Parameters.AddWithValue("@ImageName", imgname);
cmd.Connection = con;
con.Open();
UserID = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
string message = string.Empty;
switch (UserID)
{
case -2:
message = "Email already exists.\\Please choose a different Email.";
break;
case -3:
message = "Username already exists.\\Please choose a different username.";
break;
default:
message = "Registration successful. Activation email has been sent.";
SendActivationEmail(UserID);
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}
}
}
}
}
}
}
protected void txtUserName_TextChanged(object sender, EventArgs e)
{
string mainconn = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
sqlconn.Open();
SqlCommand cmd = new SqlCommand("select Username from User3 where UserName=@UserName", sqlconn);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
checkuseravilable.Visible = true;
//Imstatus.ImageUrl = "";
Litstatus.Text = "UserName Not Available !";
}
else
{
checkuseravilable.Visible = true;
//Imstatus.ImageUrl = "";
Litstatus.Text = "UserName Available !";
}
}
}