Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的注册功能有时才起作用_C#_Asp.net_Sql Server - Fatal编程技术网

C# 我的注册功能有时才起作用

C# 我的注册功能有时才起作用,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我有一个简单的注册功能,在我的项目,检查重复的电子邮件在数据库中 protected void Reg(object sender, EventArgs e) { string em = email.Value; string id = nationalid.Value; string pass = password.Value; string country = DDLcountry.SelectedItem.Text; string dateof =

我有一个简单的注册功能,在我的项目,检查重复的电子邮件在数据库中

protected void Reg(object sender, EventArgs e)
{
    string em = email.Value;
    string id = nationalid.Value;
    string pass = password.Value;
    string country = DDLcountry.SelectedItem.Text;
    string dateof = dob.Value;

    string cid = ((id.Length > 9) ? id.Substring(id.Length - 9, 9) : id);
    string uid = DDLcountry.SelectedItem.Value + cid;

    string strpass = encryptpass(pass);
    SqlCommand chk = new SqlCommand("SELECT * FROM UserData WHERE ([email] = @email)", con);
    chk.Parameters.AddWithValue("@email", em);
    con.Open();
    SqlDataReader reader = chk.ExecuteReader();
    if (!reader.HasRows)
    {
        Random rand = new Random();
        code = rand.Next(100001, 999999).ToString();
        sendCode();
        SqlCommand cmd = new SqlCommand("insert into UserData(userID, email, country, nationalID, password, activatecode, DOB)values(@userID, @email, @country, @nationalID, @password, @code, @DOB)", con);
        cmd.Parameters.AddWithValue("@userID", uid);
        cmd.Parameters.AddWithValue("@email", em);
        cmd.Parameters.AddWithValue("@country", country);
        cmd.Parameters.AddWithValue("@nationalID", id);
        cmd.Parameters.AddWithValue("@password", strpass);
        cmd.Parameters.AddWithValue("@code", code);
        cmd.Parameters.AddWithValue("@DOB", dateof);
        int j = cmd.ExecuteNonQuery();
        if (j != 0)
        {
            Response.Redirect("ActivationEmail.aspx?em=" + email.Value);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Registration Failed !! Try again !!')", true);
        }
    }
    else
    {
        //user existed
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Email Existed !! Try again !!')", true);
    }
    con.Close();
}
问题:

即使我注册,它有时也不起作用,会提示我一封电子邮件。 但是,我使用的是数据库中不存在的全新电子邮件。

您不需要SqlDataReader,只需要带有Execute.NonQuery和Execute.Scalar的SqlCommand。标量将返回一个值,您将使用该值对行进行计数,如果计数为0,则没有该电子邮件的记录,然后您可以使用Execute.NonQuery插入新电子邮件

protected void Reg(object sender, EventArgs e)
{
    string em = email.Value;
    string id = nationalid.Value;
    string pass = password.Value;
    string country = DDLcountry.SelectedItem.Text;
    string dateof = dob.Value;

    string cid = ((id.Length > 9) ? id.Substring(id.Length - 9, 9) : id);
    string uid = DDLcountry.SelectedItem.Value + cid;

    string strpass = encryptpass(pass);
    SqlCommand chk = new SqlCommand("SELECT * FROM UserData WHERE ([email] = @email)", con);
    chk.Parameters.AddWithValue("@email", em);
    con.Open();
    SqlDataReader reader = chk.ExecuteReader();
    if (!reader.HasRows)
    {
        Random rand = new Random();
        code = rand.Next(100001, 999999).ToString();
        sendCode();
        SqlCommand cmd = new SqlCommand("insert into UserData(userID, email, country, nationalID, password, activatecode, DOB)values(@userID, @email, @country, @nationalID, @password, @code, @DOB)", con);
        cmd.Parameters.AddWithValue("@userID", uid);
        cmd.Parameters.AddWithValue("@email", em);
        cmd.Parameters.AddWithValue("@country", country);
        cmd.Parameters.AddWithValue("@nationalID", id);
        cmd.Parameters.AddWithValue("@password", strpass);
        cmd.Parameters.AddWithValue("@code", code);
        cmd.Parameters.AddWithValue("@DOB", dateof);
        int j = cmd.ExecuteNonQuery();
        if (j != 0)
        {
            Response.Redirect("ActivationEmail.aspx?em=" + email.Value);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Registration Failed !! Try again !!')", true);
        }
    }
    else
    {
        //user existed
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Email Existed !! Try again !!')", true);
    }
    con.Close();
}
/* 而是数一数行。 确保将它们都修剪并小写。 */

发送代码;如果是关于插入的记录,如果j!=因此,如果记录插入成功,它将发送代码并重定向用户,但是如果用于验证的记录需要用户输入,那么应该在插入记录之前


此外,您还需要使用using块来自动处理连接

usingvar command=SqlCommandSELECT COUNT*FROM UserData中的行,其中LOWERRTRIMLTRIM[email]=@email,con have error非发票成员sqlcommand不能像method@Easonvar命令=新的SqlCommand。。。忘记了新关键字。我正在测试它,它也有错误。ExecuteScalar需要一个打开的可用连接。连接的当前状态是关闭的。@如果它现在可以工作,请检查更新。它仍然是相同的错误。仅供参考,特别是对于您的AddWithValue@DOB,dateof;-那是字符串还是日期?