我要实现添加、修改的功能,但用自定义的验证控件出现很多问题,请教一个验证方法,实现:
1.添加时判断数据库中是否有相同数据,有False,无true。(最好是3个字段)
2.修改操作同上。
解决马上给分,给时会再追加100分,这个分是给路过的。
另:希望有具体的代码及部分讲解,谢谢!

解决方案 »

  1.   

    用验证控件好像不行吧,要是我就用ajax
      

  2.   

    判断数据库中是否有相同数据
    自己写个验证的方法啊
    public static bool IsInsertOK(string ID,string name,string sex)
    {
       string sql = "select * from table where id ='"+ID+"' and name ='"+name+"' and sex ='"+sex+"'";
       ....省略一段读数据库代码
      bool ok = true;
      if(reader.read())ok=false;
      reader.close();
      return ok;
    }
      

  3.   

    ASP.NET 我不懂,不过我用ASP做过一个判断数据库里是否有相同内容的,以下是判断代码,希望对你有帮助。
    <%Set rs1=Server.CreateObject("ADODB.Recordset")
    sql1="select * from TYPE_INFO where company_id='"&company_id&"' and type_name='"&type_name&"'  and status=1"
    rs1.open sql1,oConn,1,3
    if not rs1.eof then 
    response.Write("<script language='javascript'>")
    response.Write("alert('类型名重复,请重新输入!');") 
    response.Write("window.history.go(-1);") 
    response.Write("</script>")
    response.End
    end if%>
      

  4.   

    还有我不会用ajax,请会的朋友写个例子,谢谢,解决马上给分结帖
      

  5.   

    to : robertlvqing()请写的再详细些  我倒。public static bool IsInsertOK(string ID,string name,string sex){
       string sql = "select * from table where id ='"+ID+"' and name ='"+name+"' and sex ='"+sex+"'";
       ....省略一段读数据库代码 //再加上 sqlCommand1这些东西定义你应该懂把
    this.sqlCommand1.CommandText = sql;
      System.Data.SqlClient.SqlDataReader reader;
    this.sqlConnection1.Open();
    reader = this.sqlCommand1.ExecuteReader();
    this.sqlConnection1.Close();
      bool ok = true;
      if(reader.read())ok=false;
      reader.close();
      return ok;
    }
      

  6.   

    上面错了一句 this.sqlConnection1.Close();应该写在reader.close();后面
      

  7.   

    然后你添加的时候调用if(IsInsertOK(id,name,sex))Insert() //添加操作代码
    else Update() 修改操作代码
      

  8.   

    定义SQL我知道,我指的详细点是指在调用的时候详细点,谢谢!今天争取解决,结帖
      

  9.   

    另外再调用后,如果有相同数据希望能弹出MessageBox,上面只有一个确定,和提示的错误信息。
    请robertlvqing() 不吝赐教,谢谢!
      

  10.   

    if(IsInsertOK(id,name,sex))Insert() //添加操作代码
    else Update() 修改操作代码
     这个还不够详细莫
      

  11.   

    if(!IsInsertOK(id,name,sex))
    {
    Page.RegisterStartupScript("key1","<script>alert('数据库中已有该记录!');</script>");
    return;
    }
      

  12.   

    public bool checkFavorites(string Favid,string memberid)
    {
    string sql = "select count(*) from t_Favorite where FavID='"+ Favid +"' and memberid='" + memberid +"'";
    int count = int.Parse(DbHelperSQL.GetSingle(sql).ToString());
    if(count != 0 )
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    通过这个函数判断是否有相关字段的相同记录,然后再相应你的事件(我自己的代码,所以里面的函数可能不太清楚)