0
我想检查用户名是否已经存在于数据库中,如果是,错误信息会提示“用户名已存在”。现在我有这个代码,但它不工作。程序仍然接受用户名,即使它是从数据库中复制的。有人可以帮我出去吗?这里是我的整个注册码:当用户注册时,用户名检查不起作用-asp.net
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from UserData where Username = '" + txtUser.Text + "'";
SqlCommand scm = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(scm.ExecuteScalar().ToString());
if (temp == 1) // check if user already exist.
{
Response.Write("User already existing");
}
conn.Close();
}
}
protected void btn_Registration_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into UserData(Username,Firstname,Lastname,Email,Password,CustomerType,DeliveryAddress,Zip,ContactNumber)values(@Username,@Firstname,@Lastname,@Email,@Password,@CustomerType,@DeliveryAddress,@Zip,@ContactNumber)";
SqlCommand scm = new SqlCommand(insertQuery, conn);
scm.Parameters.AddWithValue("@Username", txtUser.Text);
scm.Parameters.AddWithValue("@Firstname", txtFN.Text);
scm.Parameters.AddWithValue("@Lastname", txtLN.Text);
scm.Parameters.AddWithValue("@Email", txtEmail.Text);
scm.Parameters.AddWithValue("@Password", BusinessLayer.ShoppingCart.CreateSHAHash(txtPW.Text));
scm.Parameters.AddWithValue("@CustomerType", RadioButtonList1.SelectedItem.ToString());
scm.Parameters.AddWithValue("@DeliveryAddress", txtAddress.Text);
scm.Parameters.AddWithValue("@Zip", txtZip.Text);
scm.Parameters.AddWithValue("@ContactNumber", txtContact.Text);
scm.ExecuteNonQuery();
Session["Contact"]= txtContact.Text;
Session["Email"] = txtEmail.Text;
Session["DeliveryAddress"] = txtAddress.Text;
label_register_success.Text = ("Registration Successful!");
//Response.Redirect("Home.aspx");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
香港专业教育学院尝试过,但还是接受了相同的用户名@Heinz Siahaan –
你尝试像上面我编辑的代码?如果'(temp == 1)'改变为'if(temp> 0)',我会改变你插入用户的概率。 –
。先生。它不读取其他部分中的第二个“scm”。它说它已经被用作父范围来表示别的东西。 @ Heinz Siahaan –