Question

I am working in ASP.NET Dynamic Data web application and have an issue of passing default value while inserting/updating records. In my application all table has following common column:

  • CreatedBy (Default Value : Logged in user)
  • CreatedDate (Default Value : DateTime.Now)
  • modifiedBy (Default Value : Logged in user)
  • ModifiedDate (Default Value : DateTime.Now)

I want to keep these column hide in Insert and Edit page and want that default value will be inserted automatically in the respective column.

Please suggest me.

Thanks paul

Was it helpful?

Solution 3

solution i have implemented finally:

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
   {
      e.Values.Add("CreatedBy", HttpContext.Current.User.Identity.Name);
      e.Values.Add("CreatedDate", DateTime.Now);
    }

 protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
      e.OldValues.Add("ModifiedBy", null);
      e.OldValues.Add("ModifiedDate", null);
      e.NewValues.Add("ModifiedBy", HttpContext.Current.User.Identity.Name);
      e.NewValues.Add("ModifiedDate", DateTime.Now);
   }

OTHER TIPS

I see you are doing simple Auditing have a look at my blog post here Basic Auditing for Dynamic Data with Entity Framework 4.x hope that helps.

It's never good to do this sort of thing in the page, it's always best to do it in your data model/layer. You can use my A New Way To Do Column Generation in Dynamic Data ... to hide the columns in all pages.

Ref:
Maintaining a Log of Database Changes - Part 1

In order to maintain a history of changes to the database's data we need to record every insert, update, and delete to some sort of "history" table. In addition to capturing the data that was inserted, updated, or deleted, we also need to note what user made the modification, as well as the date and time it was made.

More Reference:

Best design for a changelog / auditing database table?
Log changes to database table with trigger

To customize ORM Entity Framework check following links:

How to use Default column value from DataBase in Entity Framework?
How to: Execute Business Logic When Saving Changes

My solution is a bit different from Paul's requirements.

In the file DynamicData\PageTemplates\Insert.aspx.cs I made edits to show my defaults for new records in any table having the shared fields. The user can still put in something else on insert.

public partial class Insert : System.Web.UI.Page
{
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e)
    {
        table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
        var values = table.GetColumnValuesFromRoute(Context);

        // set default values for meta data of new records across all tables
        // unknown values will be skipped
        values.Add("creationDate", DateTime.Now);
        values.Add("modificationDate", DateTime.Now);
        values.Add("modificationUser", HttpContext.Current.User.Identity.Name.Substring(
            HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1));

        FormView1.SetMetaTable(table, values);
        DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
    }
    [...]
}

For editing records with existing values, I made changes to some DynamicData\FieldTemplates files.

public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // ...

        // show current user as value for the modification user upon editing records
        if (Column.Name == "modificationUser")
        {
            FieldValue = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\\") + 1);
        }
    }
    [...]
}

It will show the updated value on the page for editing, but after updating the changes won't persist! An additional change to the Edit page template is required:

    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        // make sure a meta data update is always triggered by setting a different old value
        // required for the edit components
        if (e.OldValues.Contains("modificationUser"))
        {
            e.OldValues["modificationUser"] = string.Empty;
            e.OldValues["modificationDate"] = DateTime.MinValue;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top