Loaddatarow not updating row

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 system.data.dll

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)

npondAuthor Commented:

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?

npondAuthor Commented:

Didn't work, got the same error on EndLoadData()
npondAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.