Loaddatarow not updating row

Posted on 2004-11-05
Last Modified: 2011-09-20
I have a dataset with a table that has 2 columns, a primary key column of type guid, and another column of type string.

I try to call Loaddatarow on this table, giving it values (guid and string) that already exist in the table.  This should treat it is an update, but it doesn't.  It adds another row, so when I make the .EndLoadData call I get a constraint error.  This is driving me up a wall.  Does the LoadDatarow method not work when the primary key of the table is a guid?  Is there a workaround?

Here is my code:

Dim dr As DataRow
dr = Me.DBSource.Tables("Company").NewRow()
dr.Item("CompanyId") = CompanyId
dr.Item("CompanyName") = CompanyName
Me.DBSource.Tables("Company").LoadDataRow(dr.ItemArray, True)

And the error I get on EndLoadData is:

An unhandled exception of type 'System.Data.ConstraintException' occured in

Additional information: Failed to enable constraints.  One or more rows contain values violating non-null, unique, or foreign-key constraints.


I've debugged, and it does add the row, even though the guid is exactly the same.  Any help is greatly appreciated.

Question by:npond
    LVL 96

    Accepted Solution

    What are you trying to accomplish here?  I can't quite figure out, and I think that there might be a better way.

    LVL 2

    Assisted Solution

    Can you try this?

    Dim newRow(2) As Object
       ' Set the values of the array.
        newRow(0) =CompanyId
        newRow(1) = CompanyName

    Me.DBSource.Tables("Company").LoadDataRow(newRow, True)


    Author Comment


    I have an application, just a simple windows app.  I have a dataset that I store all the data in for this app, and then I save it to xml, and load it again the next time I run the app.

    The problem I have is editing records in the dataset.  The way I read LoadDataRow docs was that if the key value existed it would update that row, if the value didn't exist it would add it.  But even though I have identical guids (i.e. trying to edit a company) it always adds a new row.  This of course causes it to fail when EndLoadData() enables constraints again.

    Is there a better way to update a row based on the pk value?


    Author Comment


    Didn't work, got the same error on EndLoadData()

    Author Comment

    I feel like an idiot!  I never accepted changes after loading data from my xml file, that's why it was adding a new record instead of finding the current one.

    I split points since you guys were nice enough to respond.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    737 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

    21 Experts available now in Live!

    Get 1:1 Help Now