How to save Deleted records in DataSet?

I need to save my deleted records in my disconnected app.
This doesent work.
DataTable deletedrecords = ds.Tables[0].GetChanges(DataRowState.Deleted);
    if(deletedrecords != null)
    {
        DataSet ds = new DataSet();
        ds.Merge(deletedrecords);
        ds.WriteXml("DeletedRecords.XML");
        }

Any ideas

Thanks in advance.
Greg.


Answer this question

How to save Deleted records in DataSet?

  • WhatIsBigBang

    It looks like that should work, depending on when you actually execute that code....

    You must do the above routine after the changes are made but <I>before</I> the AcceptChanges method is called.

    How are you allowing the rows to be deleted  

  • amaly0827

    The full feature set of .NET data relys on working in "disconnected" mode.  This simply means that the data you are working with is loaded from the <i>DataBase</I> into a local, or "offline", <i>DataSet</i>.  All of the modifications made by the user occur in the DataSet.  These changes are not reflected in the DataBase until you use the DataAdapter that created the DataSet to update the DataBase with the changes.  This is what it means to work in "disconnected mode".

    If you need your application to keep track of changes, then you must monitor the changes made by the user, call GetChanges to save a copy of the differences, and then call AcceptChanges to apply them to the DataSet.  You can then later update the DataBase from the DataSet.

    Here is a code snippet from MSDN on making and monitoring changes in a datagrid:

    Private Sub EditGrid(myGrid As DataGrid)
        ' Get the selected row and column through the CurrentCell.
        Dim colNum As Integer
        Dim rowNum As Integer
        colNum = dataGrid1.CurrentCell.ColumnNumber
        rowNum = dataGrid1.CurrentCell.RowNumber
        ' Get the selected DataGridColumnStyle.
        Dim dgCol As DataGridColumnStyle
        dgCol = dataGrid1.TableStyles(0).GridColumnStyles(colNum)
        ' Invoke the BeginEdit method to see if editing can begin.
        If dataGrid1.BeginEdit(dgCol, rowNum) Then
            ' Edit row value. Get the DataTable and selected row.
            Dim myTable As DataTable
            Dim myRow As DataRow
            ' Assuming the DataGrid is bound to a DataTable.
            myTable = CType(dataGrid1.DataSource, DataTable)
            myRow = myTable.Rows(rowNum)
            ' Invoke the Row object's BeginEdit method.
            myRow.BeginEdit()
            myRow(colNum) = "New Value"

    'Here is where you would insert the code to get a copy of the changes prior to calling AcceptChanges.

            ' You must accept changes on both DataRow and DataTable.
            myRow.AcceptChanges()
            myTable.AcceptChanges()
            dataGrid1.EndEdit(dgCol, rowNum, False)
        Else
            Console.WriteLine("BeginEdit failed")
        End If
    End Sub 'EditGrid

    Now, this example is for changing values programatically.  Since you are trying to capture changes made by the user, you will have to call a modified version of this routine in the DataGrid.CurrentCellChanged event.  Something like:

    'Handle the DataGrid.CurrentCellChanged event
    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
    'See if the DataSet has changes
    If myDataSet.HasChanges Then
    'Use the GetChanges method to get a new DataSet with the changes
    'Append this new dataset to the global "DataChanges" dataset you created, or append your XML file directly
    'Call the AcceptChanges method
    End If
    End Sub

    You <B>must</B> implement the HasChanges, GetChanges, and AcceptChanges methods in your code somewhere in order to properly capture and record changes to the DataSet.

  • Gu&amp;#38;&amp;#35;240&amp;#59;mundur

    Thanks for Your Help and assistans.
    I think I got the hang of it works now.
    One of the major cause was the acceptchanges thing that I didn't do at the right time.
    Thanks again.
    /Greg

  • Skafever

    Thanks for Your answer.
    My app is mentioned to be used in an disconnected mode.
    The User first get the data from the databas online that the user want to edit offline.
    Back in office the user should be able to save the manipulated data back to the databas.
    I have moved further one with my problems to try to understand how it really works.
    I have managed to save the data back to the databas with both new/modifed and deleted records. 
    The problem is that when i read the saved xml it seems that the XmlReadMode.DiffGram doesen't work for deleted records  
    The dataset is used in a Datagrid and the delete i just done with the Delete from the keyboard.

    The if statement: if(deletedrecords != null), is  one and the dataset with "deleted" records are written to the disc, but it's empty.
    In the debug mode I can se that the records are in "DeletedRowInaccessibleException" is this the problem

    When I have done a change in the datagrid and want to save it for later use, I save the Dataset with: ds.writexml(filename, XmlWriteMode.Diffgram)

    The changes is saved in the file but the deleted stuff seems to be saved in some other form.

    Later the client can read it back with ds.readxml(filename, XmlReadMode.Diffgram)
    For the datagrid i just use the     
    dataviewmanager.DataViewSettings[0].RowStateFilter = DataViewRowState.CurrentRows; 

    What is the best approch for building an offline/disconnected app  
    Use xml  Use msde  Use what
    Thanks again.

    /Greg

  • How to save Deleted records in DataSet?