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


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.

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
>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:
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
escap-fssAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.