重复值的问题在编辑页面中检查

时间:2019-06-17 09:53:22

标签: c# sql asp.net

我检查了一些不能在数据库中重复的值es(电子邮件),这些值我也存在于编辑页面中,问题是如果我留下电子邮件,它会告诉我该电子邮件已被使用。我不知道该怎么解决。

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (checkemaill() == true)
        {

            Label35.Visible = true;
            Label35.Text = "Questa email è già stata usata";
        }
          else if (Partitaiva() == true)
         {
            Label36.Visible = true;
            Label36.Text = "La partita iva già è stata usata";
         }
          else
          {
            string query = "UPDATE Persona SET Email = @Email, 
            RagioneSociale = @RagioneSociale, Piva = @Piva WHERE ID = 
            @id";
            using (SqlConnection con = new 
            SqlConnection(ConfigurationManager.
            ConnectionStrings["dbConnection"].ToString()))
            {
                SqlCommand cmd = new SqlCommand(query, con);
                List<SqlParameter> p = new List<SqlParameter>();
                p.Add(new SqlParameter("@Email", TextBox12.Text));
                p.Add(new SqlParameter("@RagioneSociale", 
                TextBox11.Text));
                p.Add(new SqlParameter("@Piva", TextBox14.Text));
                p.Add(new SqlParameter("@ID", Id));
                con.Open();
                GetExample(cmd, p.ToArray());
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                Response.Redirect("Dash.aspx");
             }
        }
    }

这是函数,其他我不放,因为它与此相同:

    private Boolean checkemail()
    {
        Boolean emailavailable = false;
        String mycon = "Data Source=DESKTOP-LORL4DL;Initial 
        Catalog=Databasepersone;Integrated Security=True;Pooling=False";
        String myquery = "Select Email from Persona where Email='" + 
        TextBox12.Text + "'";
        SqlConnection con = new SqlConnection(mycon);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = myquery;
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            emailavailable = true;
        }
        con.Close();
        return emailavailable;
    }

2 个答案:

答案 0 :(得分:2)

问题是您的查询变为Select Email from Persona where Email='',该查询返回零条记录,表明空电子邮件地址可用,从而使您的代码得以继续。

您需要验证用户输入。使用RequiredFieldValidator,例如,请参见How to validate this data entry form in ASP.NET Web Forms?Microsoft Docs: RequiredFieldValidator。这样可以确保在电子邮件文本框为空时不会执行您的代码。

对于另一个问题,如果要检查另一个用户是否具有相同的电子邮件地址,则需要添加当前用户的ID:

SELECT Email FROM Persona WHERE Email = @Email AND Id != @Id

此外,您的代码容易受到SQL注入的攻击。而且,您不应该对连接字符串进行硬编码。

答案 1 :(得分:-1)

尝试一下:

   private Boolean checkemail(string _email)
   {
        bool emailavailable = false;

        using (your context)
        {
            var res = context.Persona.Where(p => p.Email == _email).ToList();

            if (res != null)
            emailavailable = true;
        }
        return emailavailable;
    }