Populate "CreatedBy" and "LUpdatedBy" fields at INSERT and UPDATE

Posted on 2006-05-22
Last Modified: 2010-04-23

I wanted to find out the best strategy (and the source code to implement it) to use at INSERT and UPDATE time.  I have a WinForms application developed in VS2005 and SQLServer 2000.  The current user ID is stored in a global variable g_userid and I am using a DataGrid1 bound to a daComms.

Is it better to populate CreatedBy and LUpdatedBy fields using custom InsertCommand and UpdateCommand of the daComms or should Triggers be used to do that.  Sample codes for both methods are needed and would help me a lot.

Question by:escap-fss
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    It depends if the trigger has access to the values of "CreatedBy" that should be used.
    if the trigger can do, it should do it, as this removes the coding from the application.

    otherwise, using the InsertCommand and UpdateCommand works also fine.

    Author Comment

    Thanks for the thoughts.

    As I mentioned, the values are stored in a global variable in the application.  Can you give some code samples for the Trigger AND the InsertCommand and UpdateCommand methods.

    The table's structure is as follows:

    CommID (identity)
    CommRem (text)
    CommRecvd (date)
    CreatedBy (text)
    LUpdatedBy (text)

    The CreatedBy and LUpdatedBy fields are in the same table where the record is being inserted.

    LVL 142

    Accepted Solution

    >As I mentioned, the values are stored in a global variable in the application.
    then, you cannot use the trigger solution.

    see here for a sample how the insertcommand can be done:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now