May 23 2008

Update a LinqDataSource in code


For nearly every table in the current app I'm working on (cjTrack National - a media tracking application for national advertising on cable networks), there are modifiedby and modifieddate fields.  Those fields, rather obviously, reflect the last user to modify the record, and most recent modification date.  I'd like to dump them, as I personally find them useless, but the business lead for the project likes being able to use them to yell at people for screwing stuff up. :)

Anyway, when editing the LinqDataSource via a GridView, these fields are set as ReadOnly, as I don't want the modifier changing the fields.  As such, I had to find a way to update them outside the parameters of the GridView.

In the past, with either a SqlDataSource or an ObjectDataSource, I would simply use the GridView Updating event, and throw the UpdateParameters the values I wanted.  Easy enough.  But, as far as I can tell, you can't really do that with the LinqDataSource, because it's parameter-less.

So, I used the Updating event for the LinqDataSource instead, and inherited the updating object with e.NewObject and then set the fields.  A note here, watch for optimistic concurrency issues, especially with a datetime field.

Code follows:

   1: NetworkOwner uOwner = (NetworkOwner)e.NewObject;
   2: uOwner.modifieddate = System.DateTime.Now;
   3: uOwner.modifiedby = HttpContext.Current.User.Identity.Name.ToString();


May 20 2008

Welcome to the cj Development Blog, ahem, version 2...


Important thing to learn about virtual servers: you have to have both the vhd files AND the snap shot files to rebuild...  Yeah, oops. 

Anyway, the blog is now back, unfortunately sans our previous posts, but we'll see what we can do about that going forward.