[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 968
  • Last Modified:

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.

  • 3
2 Solutions
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.

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.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now