LinqDataSource DataContext Pending changes


  • I have a GridView that has a DataSourceID for a LinqDataSource with a Linq to SQL DataContext (called DataClassesDataContext) in the ContextTypeName.

    I want to be able to insert, update and delete in the gridview without saving directly in the database, that should only happen when you click the Save button. From what i've read the LinqDataSource creates a DataClassesDataContext instance, but i don't think it's a permanent datacontext and i can't access it except on the ContextCreated event.

    How can i do "pending changes" if i don't have a permanent datacontext and i can't access the DataClassesDataContext of the LinqDatasource to allow me to call InsertOnSubmit (to create new entries), DeleteOnSubmit(to remove entries) and to update whatever needs to be updated and then call SubmitChanges() in the Save button click event?

    Wednesday, August 08, 2012 5:06 PM

All replies

  • Hi,

    I think you can set the correct datacontext first when the OnContextCreating event fired: http://stackoverflow.com/questions/924312/updating-linqdatasource-from-separate-datacontext

    Then you can use LinqDataSourceStatusEventArgs.Result Property to get the datacontext: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.result.aspx#feedback

    Friday, August 10, 2012 2:42 AM
  • Yes i can set on the OnContextCreating, but that datacontext has to be persistent so that he knows all of the changes that i've made until i clicked on the Save button where i call SubmitChanges(), and from what i've tried it's not, it's always a fresh one with every postback/action and i can't get it to save in the Viewstate because it's not serializable. Another thing is that i can only get the datacontext from LinqDataSourceStatusEventArgs.Result property like you said, but i can't access it in a btnSave_Click event that i need to call SubmitChanges().

    • Edited by xlar8or Friday, August 10, 2012 9:27 AM
    Friday, August 10, 2012 8:58 AM
  • Hi,

    I think you can create a cache class as a global variable to save the changes temporary data. 

    Monday, August 13, 2012 9:15 AM
  • pls try this

    List<Class> ls = new List<Class>();

    public void Insert()


    ls.Add( new Class { } );


    public void SaveButton_ClickEvent( object sender, EventArgs e )


    Classes.InsertAllOnSubmit( ls);


    Wednesday, March 27, 2013 5:00 PM
  • One option could be loading your data from database to the sessions, connect grid datasource to session, do all your modifications and on final save click push session values into the database.

    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    Thursday, March 28, 2013 11:23 AM