I have a table for Contacts that I’m storing for customers with columns:
- Addressline1
- Addressline2
- CityID
- EmpID (FK)
- ModifiedDate (Computed, DateTime)
I use ASP.NET 4.0 and in the form I have used ASP.NET wizard with 3 steps. I have few problems.
- How do I insert
EmpIDinto a tabledbo.ContactswhereEmpIDis a FK. - How to store
ModifiedDate? What is the sqlparameter to add the Time value?
You have basically two choices to keep a
ModifiedDateup to date:define a
DEFAULTconstraint on that column to get the current date/time when you freshly insert a row, and create anAFTER UPDATEtrigger to update theModifiedDateafter every update operation on that rowset the
ModifiedDatein your client application before every save operation to the databaseUpdate: if you want to create an
AFTER UPDATEtrigger, use code something like this:Here, I’m assuming your table
YourTableHerehas some kind of a primary key – a column like anIDthat uniquely and clearly identifies each single row. Based on that row, you can update theModifiedDateof all those rows that have been updated by a given SQL statement.Insertedis a pseudo table that’s available inside a trigger code body, which contains all the rows that have been modified by the operation that caused this trigger to fire (all rows affected by yourUPDATEstatement onYourTableHere)Read more about triggers here:
Update #2: if your
ModifiedDatecolumn is in fact computed on the SQL Server table level, you cannot update it directly. You’ll need to find out what it’s computed from, and then you need to modify that column (if you can).To find out what it’s computed from, run this query on your SQL Server database: