Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving values of ReadOnly fields from DynamicData DetailsView in Edit Mode on Updating using LinqDataSource
    primarykey
    data
    text
    <p>I have several tables in my database that have read-only fields that get set on Inserting and Updating, namely: AddDate (DateTime), AddUserName (string), LastModDate (DateTime), LastModUserName (string).</p> <p>All of the tables that have these values have been set to inherit from the following interface:</p> <pre><code>public interface IUserTrackTable { string AddUserName { get; set; } DateTime AddDate { get; set; } string LastModUserName { get; set; } DateTime LastModDate { get; set; } } </code></pre> <p>As such, I have the following method on the Edit.aspx page:</p> <pre><code>protected void DetailsDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e) { IUserTrackTable newObject = e.NewObject as IUserTrackTable; if (newObject != null) { newObject.LastModUserName = User.Identity.Name; newObject.LastModDate = DateTime.Now; } } </code></pre> <p>However, by the time it hits this method, the e.OriginalObject has already lost the values for all four fields, so a ChangeConflictException gets thrown during the actual Update. I have tried adding the four column names to the DetailsView1.DataKeyNames array in the Init event handler:</p> <pre><code>protected void Page_Init(object sender, EventArgs e) { // other things happen before this var readOnlyColumns = table.Columns.Where(c =&gt; c.Attributes.SingleOrDefaultOfType&lt;ReadOnlyAttribute&gt;(ReadOnlyAttribute.Default).IsReadOnly).Select(c =&gt; c.Name); DetailsView1.DataKeyNames = DetailsView1.DataKeyNames.Union&lt;string&gt;(readOnlyColumns).ToArray&lt;string&gt;(); DetailsView1.RowsGenerator = new CustomFieldGenerator(table, PageTemplates.Edit, false); // other things happen after this } </code></pre> <p>I've tried making that code only happen on PostBack, and still nothing. I'm at a lose for how to get the values for all of the columns to make the round-trip.</p> <p>The only thing the CustomFieldGenerator is handling the ReadOnlyAttribute, following the details on <a href="http://csharpbits.notaclue.net/2009/01/making-field-read-only-via.html" rel="nofollow">C# Bits</a>.</p> <p>UPDATE: After further investigation, the values make the round trip to the DetailsView_ItemUpdating event. All of the values are present in the e.OldValues dictionary. However, they are lost by the time it gets to the LinqDataSource_Updating event.</p> <p>Obviously, there are the "solutions" of making those columns not participate in Concurrency Checks or other ways that involve hard-coding, but the ideal solution would dynamically add the appropriate information where needed so that this stays as a Dynamic solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload