So,I figured out that there is a FormView_ItemInserted Event which is fired once insert link button is clicked.I believe, I can pick the values from here and update another tables based on these values.Please correct me if I am wrong.
Thanks for the reply.I do need to write custom logic so that the custom code is executed only when values are being inserted into a particular table.To do so do I need to create a separate <tablename>.cs file in the App_Code folder? If that is the case then
how do I retrieve the values being inserted via the FormViewInsertedEventArgs reference.
Hi Simpleguy, are you using Linq to SQL or Entity Framework?
In Linq to SQL
public partial class NorthwindDataContext
{
partial void InsertEmployee(Employee instance)
{
// do business logic here
ExecuteDynamicInsert(instance);
}
}
Note the simple approach of Linq to SQL you have a seperate method for each entity and action on that entity.
In Entity Framework
public partial class NorthwindEntities
{
partial void OnContextCreated()
{
// Register the handler for the SavingChanges event.
this.SavingChanges += new EventHandler(context_SavingChanges);
}
// SavingChanges event handler.
private static void context_SavingChanges(object sender, EventArgs e)
{
var objects = ((ObjectContext)sender).ObjectStateManager;
// Get new objects
foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added))
{
// Find an object state entry for a SalesOrderHeader object.
if (entry.Entity.GetType() == typeof(Employee))
{
var usr = entry.Entity as Employee;
// Do your Business Logic here.
}
}
}
}
Things are a little more complicated here as you have to get a list of entities for a particular action (Insert Edit & Delete and others) then test for the entity type you want to work upon.
Hope this helps [:)]
Dynamic DataBusiness Logic
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
public partial class NorthwindDataContext
{
partial void InsertEmployee(Employee instance)
{
// do business logic here
ExecuteDynamicInsert(instance);
}
}
Note the simple approach of Linq to SQL you have a seperate method for each entity and action on that entity.
Thank you very much for the reply.I am using LINQ to SQL.I'll try it out and get back to you in case of further queries.
Just one more quick question.I am assuming I need to create a new .cs file and name it according to the name provided in the Global.asax file.Also,is Employee a table name in the Northwind database?and does InsertEmployee need to be called from somewhere
or it'll be invoked automatically?
Hi Simpleguy, sorry missed you previous reply [:$] anyway what you do depends on weather you are using an Web Application Project of a file based Website?
In a Web Application project you create a new class file in the same folder as your Model (I use Models folder) and make sure that it has the same namespace as the model.
In a file based Website you put the class file into the App_Code folder where you model is already and namespace should not be an issue.
Note: if the model is in a seperate class library then the metadata and DataContexts partials should be in that class library.
Dynamic Data
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
simpleguy
Member
14 Points
91 Posts
Re: How to do an update on an insert operation using dynamic data
Jul 13, 2010 05:19 AM|LINK
So,I figured out that there is a FormView_ItemInserted Event which is fired once insert link button is clicked.I believe, I can pick the values from here and update another tables based on these values.Please correct me if I am wrong.
vishwaraj1
Contributor
3687 Points
861 Posts
Re: How to do an update on an insert operation using dynamic data
Jul 13, 2010 03:11 PM|LINK
inserted event is fired when any record is inserted.. for updation u you can use other events. there are updating
events also for the form view.
Vishwaraj Malik
Skype ID: vishwaraj.malik
VB to C# Converter
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
simpleguy
Member
14 Points
91 Posts
Re: How to do an update on an insert operation using dynamic data
Jul 13, 2010 03:35 PM|LINK
Thanks for the reply.I do need to write custom logic so that the custom code is executed only when values are being inserted into a particular table.To do so do I need to create a separate <tablename>.cs file in the App_Code folder? If that is the case then how do I retrieve the values being inserted via the FormViewInsertedEventArgs reference.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: How to do an update on an insert operation using dynamic data
Jul 14, 2010 10:31 AM|LINK
Hi Simpleguy, are you using Linq to SQL or Entity Framework?
In Linq to SQL
public partial class NorthwindDataContext { partial void InsertEmployee(Employee instance) { // do business logic here ExecuteDynamicInsert(instance); } }Note the simple approach of Linq to SQL you have a seperate method for each entity and action on that entity.
In Entity Framework
public partial class NorthwindEntities { partial void OnContextCreated() { // Register the handler for the SavingChanges event. this.SavingChanges += new EventHandler(context_SavingChanges); } // SavingChanges event handler. private static void context_SavingChanges(object sender, EventArgs e) { var objects = ((ObjectContext)sender).ObjectStateManager; // Get new objects foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added)) { // Find an object state entry for a SalesOrderHeader object. if (entry.Entity.GetType() == typeof(Employee)) { var usr = entry.Entity as Employee; // Do your Business Logic here. } } } }Things are a little more complicated here as you have to get a list of entities for a particular action (Insert Edit & Delete and others) then test for the entity type you want to work upon.
Hope this helps [:)]
Dynamic Data Business Logic
Always seeking an elegant solution.
simpleguy
Member
14 Points
91 Posts
Re: How to do an update on an insert operation using dynamic data
Jul 14, 2010 02:23 PM|LINK
Thank you very much for the reply.I am using LINQ to SQL.I'll try it out and get back to you in case of further queries.
simpleguy
Member
14 Points
91 Posts
Re: How to do an update on an insert operation using dynamic data
Jul 14, 2010 02:43 PM|LINK
Just one more quick question.I am assuming I need to create a new .cs file and name it according to the name provided in the Global.asax file.Also,is Employee a table name in the Northwind database?and does InsertEmployee need to be called from somewhere or it'll be invoked automatically?
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: How to do an update on an insert operation using dynamic data
Jul 14, 2010 02:55 PM|LINK
Hi Simpleguy,
what are you using Linq to SQL or Entity Framework?Dynamic Data
Always seeking an elegant solution.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: How to do an update on an insert operation using dynamic data
Jul 14, 2010 03:01 PM|LINK
Hi Simpleguy, sorry missed you previous reply [:$] anyway what you do depends on weather you are using an Web Application Project of a file based Website?
Note: if the model is in a seperate class library then the metadata and DataContexts partials should be in that class library.
Dynamic Data
Always seeking an elegant solution.