Thread: AutoGenerate Employee Id with Department As Prefix

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    AutoGenerate Employee Id with Department As Prefix

    I want to autogenerate employee id when user enters details of employee
    Conditions:
    User Selects Department which should be used as prefix.

    A new Sequence of numbers must be generated for each department

    Example:
    DeptA0001
    DeptA0002
    Again DeptB new seuquence must be started
    DeptB0001
    DeptB0002...

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So? You want it - you do it.
    You encounter a problem - you post your code and describe the problem.

    We help you to solve the problem with your code.

    If you need more details on the process - read homework policy.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This is usually taken care of by SQL Server. Usually you are going to store the information in one or more tables in a relational database. When you add a row to a table within a dataset in ADO .NET the row will be incremented or decremented based on the table settings in the dataset designer in MSVS. The default setting is to start at -1 and decrease by 1 for each new row. When these are submitted to SQL Server it will see the negative row IDs and will auto assign new row IDs based on its own internal rules. This is ideal because it allows the database to figure out a unique ID and allows the programmers to simply add, remove and edit rows without having to worry about the nitty gritty process of storing the rows in the database. The code to add a row and submit amounts to about 3 lines of C#.

    All this depends solely on how you have your database, stored procedures (if any) and datasets set up. There are a plethora of options in all of this so I won't go into them. I have described the most basic and default way this is handled in ADO, MSVS and SQL Server. Each company does this differently. Some will use stored procedures for everything while others will take a pure code approach. I prefer stored procedures since it removes most of the database specific logic from your code.

    Note that you can take advantage of datasets and data tables without having a database set up and they work the same way.
    Last edited by VirtualAce; 07-08-2013 at 11:52 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What database?

    In MS SQL Server generating an ID number is known as an 'identity', Oracle is a 'sequence', Access is 'autonumber', etc.

    Some older DBs use a 1 row table to hold these ID numbers (ie only 1 row with one field per department that holds the next un-used ID value and one field for the department's prefix 'DeptA', 'DeptB', etc).
    Often stored proc is sent the department and returns the appropriate ID value (as well as updating the table to the next ID value).

    I would also probably just store the 'department name' (ie 'DeptA') and 'ID number' (ie 12345) in individual fields in a TABLE (as well as an primary key ID field).
    Then I would use a VIEW to concatinate those fields and expose all the data to a user (ie 'DeptID' = 'department name' + 'ID number')
    I would add an INSERT trigger to the VIEW to handle creating this data for new employees (and UPDATE, DELETE etc as required).

    But any design depends on volume of data, frequency of change, speed required, etc which you have not supplied.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The more things change department
    By Adak in forum C Programming
    Replies: 4
    Last Post: 10-12-2011, 02:00 PM
  2. Sales department salary range generator
    By erm3r in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2011, 07:34 AM
  3. Replies: 0
    Last Post: 05-21-2005, 09:42 PM
  4. You might be a microsoft employee if...
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-02-2002, 08:14 PM
  5. Interview with M$ employee
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 08-17-2002, 04:44 AM