我用了一个自定义验证控件,输入的工号不能重复.
如果重复了就会添加到数据库失败.但是现在控件会提示我工号已经存在,也能写入到数据库,请大家帮忙看一下问题在哪里.
谢谢!~
相关代码如下:  
 public static bool findPerson(string gh)
    {
        SqlConnection con = addpersonOperate.createCon();
        con.Open();        SqlCommand cmd = new SqlCommand("select count(*) from personlist where gh='" + gh + "'", con);        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        
        string gh = args.Value;        if (addpersonOperate.findPerson(gh))
        {            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }protected void Button1_Click(object sender, EventArgs e)
    {
        addperson p = new addperson();
        
        
        p.gh = this.gh.Text;
        
        if (addpersonOperate.insertOperate(p))
        {
              //Response.Write("添加成功");
           Response.Write("<script> alert('添加成功') </script>");
        }
        else
        {
            //Response.Write("添加失败");
         Response.Write("<script> alert('添加失败') </script>");
        }
    }