locked
Updating datarow fails on new row ado.net RRS feed

  • Question

  • User119554597 posted

    1. I create a new data row in table that is detached and contains all the records of that table

    2. I fetch the id of the data row and update the row with it, then I accept it without updating the database.

    3. I then change a field in the data row and update.

    I get a "An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in System.Data.dll"

    why?

    Here is the code it goes through after the table is filled:

    ------------------------

    Dim curPerson As XPDataSRCDataSet.PersonsRow = CreatePersonRow(Add.ID) ' Creates the new person row updates the database, fetches its ID, and updates the id accepting the results.

            Dim Add As XPDataSRCDataSet.AddressesRow = AddressDS.Addresses.Rows(0)
            Dim curPerson As XPDataSRCDataSet.PersonsRow = CreatePersonRow(Add.ID)
            Dim miua = AddressDS.Persons.FindByID(curPerson.ID)
            curPerson.FirstName = "Miau"
            Dim miua1 = AddressDS.Persons.FindByID(curPerson.ID)
            Call UpdateSinglePersonRow(curPerson) 'Used only because all db access is in a single class.
            Dim miau2 = AddressDS.Persons.FindByID(curPerson.ID)
            Stop
    Public Function CreatePersonRow(AddID As Integer)
        Public Sub UpdateSinglePersonRow(row As XPDataSRCDataSet.PersonsRow)
            PersonsTableAdapter.Update(row)
        End Sub

    Dim newPerson = AddressDS.Persons.NewPersonsRow With newPerson .Updated = Now .Created = Now .AddId = AddID .Appelation = "Mr" .FirstName = "UNKNOWN" .LastName = "UNKNOWN" End With AddressDS.Persons.AddPersonsRow(newPerson) PersonsTableAdapter.Update(newPerson) newPerson.ID = PersonsTableAdapter.GetNewID 'Fetches the max id current in the database for persons newPerson.AcceptChanges() Return newPerson End Function



    Monday, April 27, 2015 12:35 AM

Answers

  • User119554597 posted

     Thank you very much the article explains how this can happen when using a stored procedure on an SQL system.

    Unfortunately, I am dealing with an ACCESS database, and the SQL solution involving Set NoCount off does not apply.

    Leon

    PS. I did manage a work around.

    1. I create a new row in the ACCESS database

    2. I fetch the id of the new row (and it takes a while for the update to take place)

    3. I fetch the new row into a separate table

    4. I import the row into the table I am using.

    That solves the problem, and keeps me from using an Interrupt handler to update the id.  That also seems to work, but I don't like it (too many interrupts makes debugging a pain).

    Leon

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2015 2:43 PM

All replies

  • User61956409 posted

    Hi rogsonl,

    Thanks for your post.

    I get a "An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in System.Data.dll"

    Call UpdateSinglePersonRow(curPerson)

    This problem can occur if the stored procedure runs the SET NOCOUNT ON statement. This article explained about this issue, please refer to it.

    Best Regards,

    Fei Han

    Monday, April 27, 2015 11:07 PM
  • User119554597 posted

     Thank you very much the article explains how this can happen when using a stored procedure on an SQL system.

    Unfortunately, I am dealing with an ACCESS database, and the SQL solution involving Set NoCount off does not apply.

    Leon

    PS. I did manage a work around.

    1. I create a new row in the ACCESS database

    2. I fetch the id of the new row (and it takes a while for the update to take place)

    3. I fetch the new row into a separate table

    4. I import the row into the table I am using.

    That solves the problem, and keeps me from using an Interrupt handler to update the id.  That also seems to work, but I don't like it (too many interrupts makes debugging a pain).

    Leon

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2015 2:43 PM