Menu

check whether a field is unique

T.Mo
2005-04-12
2013-03-07
  • T.Mo

    T.Mo - 2005-04-12

    Need help for following issue:

    My project has two fields: Email, ICNumber. while adding a new entry into table, i need to check that this two fields must not existed in the database.

    One way is to create a criteriacondition, doing a selection.

    Is there a better way? the Find Function? I realize if i assign the two fields to find="true", and use like following code, it didnt work:

    Customer cst = new Customer();
    cst.Email = "we@gmail.com";
    if (cst.find())
    {
        Info.Text = "Email existed";
        return;
    }

    cst = new Customer();
    cst.ICNumber = "11111";
    if (cst.find())
    {
        Info.Text = "IC Number Existed";
        return;
    }

    -----------------------------------

    the problem is the cst.find() never return true even the same email existed.

     
    • Richard Banks

      Richard Banks - 2005-04-12

      The find method uses all of the attributes with find="true" to locate an object (just like as compound key).

      If you run the program in debug you would see the where condition of the SQL clause being something like

      select ... where t1.email=@p1 and t1.icnumber=@p2

      The retrievecriteria is the best option. (VB code follows)

      dim rc as CRetrieveCriteria
      rc.classmap = cst.GetClassMap
      rc.WhereCondition.addSelectEqualTo("Email",cst.Email)
      'The third parameter being true adds the equality check using an OR condition
      rc.WhereCondition.addSelectEqualTo("ICNumber",cst.Email,True)
      dim cc as CCursor = rc.perform()
      if cc.HasElements then
        info.text = "Email or ICNumber is in use"
      end if

      the sql where clause generated would be something like

      select ... where t1.email=@p1 or t1.icnumber=@p2

      I hope that is what you are after.
      - Richard.

       
    • T.Mo

      T.Mo - 2005-04-12

      Thanks. Richard. That is what I need.

      I just misunderstood the usage of find function.

      I have learnt a lot abt the framework by using it. but still, i feel that a better tutorial or a better sample application shall be provided in your web site. Maybe I can contribute a bit on this.

      Regards,
      Timothy

       
      • Richard Banks

        Richard Banks - 2005-04-12

        Any help is appreciated.

        Documentation is the next thing I'm working on.  I'm going to make a release candidate of the latest code available soon, and will then get stuck into knocking out some proper documentation before a official release of the next version.

        A users guide has been started (usersguide.sh5 in CVS) using HelpMaker from vizacc.  Feel free to download it and look at whats in there and the structure I'm aiming for.  I need feedback to make sure I'm going in the right direction.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.