3

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

Konstantin
  • 796
  • 1
  • 11
  • 32
Paul
  • 457
  • 2
  • 11
  • 26

4 Answers4

2

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.

Wizzard
  • 924
  • 5
  • 9
  • For auditing we have used sqlserver 2008 auditing. I just wanted to hide these field from UI. As createdby/CreatedDate is mandatory field in DB using scaffolding=false was not possible. Finally i resolved it by adding these field/value in FormView1_ItemInserting event. Anyway thanks for the information. I have read numbers of article written by you on DD and got lots of help. Thanks a lot>> – Paul Apr 20 '12 at 15:37
0

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

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • thanks for your quick response. For history we have separate transaction table. what i need is suppose i do not want to display a column in insert/edit dynamic data page even that column is NOT NULL in DB level. Now while inserting/updating record of that entity i want to pass some default value in insert/update query. – Paul Apr 16 '12 at 16:04
  • Okk..when you are creating tables then specify default value for columns or use stored procedure specifying default values.... – Niranjan Singh Apr 16 '12 at 16:22
  • it is not possible to specify default value in table as createdby/modified is variable(loggedin user). I am using dynamicdata with EF and DD automatically do insert/updated job so no scope of writing SP. Is there any workaround in DD??? – Paul Apr 17 '12 at 01:52
  • check the added links for entity framework for reference on this topic – Niranjan Singh Apr 17 '12 at 05:39
0

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);
   }
Paul
  • 457
  • 2
  • 11
  • 26
  • A more thorough explanation of what you're doing: [Updating Values Before Insert on ASP.NET Dynamic Data Website Using Entity Framework](http://batesits.com/tag/dynamic-data/) – ofthelit Jan 26 '16 at 14:03
0

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;
        }
    }
ofthelit
  • 1,341
  • 14
  • 33