Getting the next value for a primary key column

Posts   
 
    
scottbed
User
Posts: 3
Joined: 16-Apr-2010
# Posted on: 30-Jun-2010 02:21:59   

I have inherited some code from another developer and I'm very confused on the proper technique for getting the next value for an integer type primary key field. Version 2.6

Here is the code that exists but it fails on some calls based on which field I'm looking for:


 public static int GetNewKey(string keyName)
         {
             var utlKeysEntity = new UtlkeysEntity(keyName);
             if (utlKeysEntity.IsNew)
             {
                 utlKeysEntity.KeyName = keyName;
                 utlKeysEntity.CurrentValue = 0;
             }
             utlKeysEntity.CurrentValue++;
             utlKeysEntity.Save();
             return utlKeysEntity.CurrentValue;
        }

The exception is in the set for the KeyName property of the UtlkeysEntity class. "The value specified will cause an overflow error in the database. Value length: 13. Column max. length: 10 Parameter name: KeyName"

The key I'm passing in is 13 characters (DescriptionID).

I am also inheriting a database and don't want to modify all the tables to use identities. I can't see how this code can work, what if you have two tables with the same primary key field name?

Thanks!!!

scottbed
User
Posts: 3
Joined: 16-Apr-2010
# Posted on: 30-Jun-2010 02:35:42   

scottbed wrote:

I have inherited some code from another developer and I'm very confused on the proper technique for getting the next value for an integer type primary key field. Version 2.6 Thanks!!!

I want to kill myself...this is a table the developer created to track the current maximum value for key fields.

This is not an LLBLGen object.

Sorry. Please ignore the code snippet above.

But the question still stands, what is the best way to do something like this using LLBLGen?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Jun-2010 08:28:53   

Please explain your question in more details.

What is the type of your PK database field? Is it an Identity Column?

Why do you want to know the Next Value?