How do I ... uniqueness of field value in table

Posts   
 
    
quentinjs avatar
quentinjs
User
Posts: 110
Joined: 09-Oct-2009
# Posted on: 28-Oct-2009 19:58:06   

I am looking to ensure that a field is unique in a table.

For example. In a user table I want to know that the UserName is unique.

So the query would normally be "Select count(UserName) as UC From Table2 Where UserName = @UserName"

then I would do something like if UC <> 0 then raise exception.

Assumptions: 1. New Record or 2. UserName has changed in the application but not yet saved to DB.

How would I create this in LLBL ? Any examples you can forward me to?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2009 03:04:41   

You could perform a Scalar query with count:

TableCollection coll = new TableCollection();
int numerOfRecords = (int)coll.GetScalar(TableFieldIndex.TableId, 
    null, AggregateFunction.Count, 
    (TableFlields.UserName == 'someValue'));
David Elizondo | LLBLGen Support Team