none
how to check which table inside the dataset has the changes RRS feed

  • Question

  • so i am using the haschanges to check if the dataset has changes and update accordingly

     If objdsOrders.HasChanges(DataRowState.Added) Then
                    added = objdsOrders.Tables(1).GetChanges(DataRowState.Added)
                    If added.Rows.Count > 0 Then
                        For Each row As DataRow In added.Rows

    the problem is that i have two tables inside the dataset

    and not always the changes happens in both tables

    so when i update i write code to try and update both tables and enclose each table under try-end try so ii dont get exceptions and yet i get exception for this line if this table is not changed

        If added.Rows.Count > 0 Then

    the exception says

    Exception thrown: 'System.NullReferenceException' in ShipmentsApp.exe

    Additional information: Object reference not set to an instance of an object.

    my question is how to know which table got the update after checking if the dataset.haschanges=true

    thanks


    I am A Medical Doctor

    Sunday, January 28, 2018 8:25 AM

All replies

  • my question is how to know which table got the update after checking if the dataset.haschanges=true

    You can handle the exception:
    https://msdn.microsoft.com/en-us/library/ms973849.aspx

    or you can test the variable for Nothing and exit the routine without trying to look at the count property:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing

    Sunday, January 28, 2018 9:01 AM
  • There is not haschanges on the table adapter, so your method is the only one. 

    However, what do you use to update the dataset, is it a tableadaptermanager then there is no problem.

    With the base of it DBDataAdapter (DB for OleDB or SQL) it is something different, with that not only what you update but as well the order that you update parents and childs is important. That is the reason the TableAdapterManager was made.

    Be aware that the adapters don't update non changed rows at all. That is a strange misunderstanding. If you read much on Internet there are petards with which I was also hoisted in past which tells you it is different.


    Success Cor


    Sunday, January 28, 2018 9:50 AM
  • Hello,

    GetChanges will return a DataTable or nothing e.g. let's look at a pattern to go with.

    Full code can be found in my MSDN code sample. Note the code sample is in SQL-Server but works with MS-Access, Oracle etc.

    Dim ds As New DataSet
    Dim addedTable As DataTable = Nothing
    Dim modifiedTable As DataTable = Nothing
    
    
    For Each table As DataTable In ds.Tables
        addedTable = table.GetChanges(DataRowState.Added)
        If addedTable IsNot Nothing Then
            ' we have one or more added records
        Else
            ' no new records
        End If
        modifiedTable = table.GetChanges(DataRowState.Modified)
        If modifiedTable IsNot Nothing Then
            ' we have one or more modified records
        Else
            ' no modified records
        End If
    Next

    Now you can do this with deleted records to yet they need special care. I have a a class and language extensions to assist with this. Call AllChanges on a DataTable, to see (without errors) if one or rows has been added use HasNew, if HasNew returns true then you can safely access Added (which is a DataTable of added rows), same goes for modified rows. For deleted I set it up to provide the primary keys so we can work with the backend table in the database.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, January 28, 2018 10:33 AM
    Moderator
  • so i am using the haschanges to check if the dataset has changes and update accordingly

     If objdsOrders.HasChanges(DataRowState.Added) Then
                    added = objdsOrders.Tables(1).GetChanges(DataRowState.Added)
                    If added.Rows.Count > 0 Then
                        For Each row As DataRow In added.Rows

    the problem is that i have two tables inside the dataset

    and not always the changes happens in both tables

    so when i update i write code to try and update both tables and enclose each table under try-end try so ii dont get exceptions and yet i get exception for this line if this table is not changed

        If added.Rows.Count > 0 Then

    the exception says

    Exception thrown: 'System.NullReferenceException' in ShipmentsApp.exe

    Additional information: Object reference not set to an instance of an object.

    my question is how to know which table got the update after checking if the dataset.haschanges=true

    thanks


    I am A Medical Doctor

    Hi Medical Doctor,

    You can use DataSet.GetChanges Method (DataRowState) to produce a second DataSet object that contains only the changes introduced into the original. Then you can get how many tabels in the dataset  are modified.

     Dim changedataset As DataSet = ds.GetChanges(DataRowState.Added)
            If changedataset.Tables.Count > 0 Then
                For Each dt As DataTable In changedataset.Tables
    
                Next
            End If

    Here is the DataSet.GetChanges method, please refer to:

    https://msdn.microsoft.com/en-us/library/0f8054fy(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 31, 2018 6:36 AM
    Moderator