Datatable.Merge not sending updates to DB

Posted on 2008-11-13
Last Modified: 2013-12-17
i ve got a  Customers datatable. I can update the DB via dataadapter with no problems.
THen I open a second isntance and i update teh same record so i get a cocurrency error. Until here everything is fine.

What im trying to do is to merge the data ive changed into the datatable holding the current data but it is not updating the database after i merge them.
any ideas?
Question by:arcross
    LVL 23

    Expert Comment

    by:Christopher Kile
    I'd need to see the code, but I'm guessing you haven't called .AcceptChanges, or you haven't properly defined .InsertCommand or .UpdateCommand.  Could you please attach the relevant code in a code snippet and send it back with your reply?
    LVL 8

    Author Comment

    Public Function UpdateRecords() As Boolean
                 da = New SqlDataAdapter
                da.UpdateCommand = UpdateCustomer()
                da.ContinueUpdateOnError = True
                Return True
    End Function
    Private Function UpdateCustomer() As SqlCommand

            Dim cmd As SqlCommand
            Dim cn As SqlConnection
            cn= New SqlConnection(cnStr)
            cmd = New SqlCommand("spUpdateCustomer", cn)
            cmd.CommandType = CommandType.StoredProcedure

            Dim param As SqlParameter
            param = cmd.Parameters.Add("@CName", SqlDbType.NVarChar, 50, "CName")
            param.SourceVersion = DataRowVersion.Current
            param.Direction = ParameterDirection.Input

            param = cmd.Parameters.Add("@CPhone", SqlDbType.NVarChar, 13, "CPhone")
            param.SourceVersion = DataRowVersion.Current
            param.Direction = ParameterDirection.Input

            '// Original Parameters
            param = cmd.Parameters.Add("@Orig_CName", SqlDbType.NVarChar, 50, "CName")
            param.SourceVersion = DataRowVersion.Original
            param.Direction = ParameterDirection.Input

            param = cmd.Parameters.Add("@Orig_CPhone", SqlDbType.NVarChar, 13, "CPhone")
            param.SourceVersion = DataRowVersion.Original
            param.Direction = ParameterDirection.Input
            '// ID
            param = cmd.Parameters.Add("@CustomerID", SqlDbType.Int, 4, "CustomerID")
            param.Direction = ParameterDirection.Input
            param.SourceVersion = DataRowVersion.Original

            Return cmd
        End Function


                If tbl.HasErrors Then
                    Dim conflict As New frmConflict2(tbl)   '// OPEN CONFLICT FORM TO RESOLVE ERRORS
                    If conflict.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                             tbl.Merge(conflict.dt_DBDataFiltered, True)
                                 UpdateRecords()  '// UPDATE AGAIN
                    End If
                End If
    TBL (Datatable)=  Is filled with data from database when the form opens for first time.
    conflict.dt_DBDataFiltered = Returned from the conflict form. It contains the rows that have been modified to new values. The ones The second instance tries to update. It contained last version of data from the database.

    thanks for your help
    LVL 41

    Accepted Solution

    It's the AcceptChanges() that's causing the problem....   comment out that line, and you'll be golden
    That's a poorly named method that doesn't do what it appears it might do.   A better name (somewhat facetiously) is LetsPretendThatChangesHaveAlreadyBeenMadeAndEraseAllRowStateMarkers()
    LVL 23

    Assisted Solution

    by:Christopher Kile
    Rather than graye's solution, I would transpose the UpdateRecords() call with the tbl.AcceptChanges() call - this way, the changes would be marked as Original in the DataSet, reflecting that the once-current values have now been posted to the database.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now