Link to home
Start Free TrialLog in
Avatar of npond
npond

asked on

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").BeginLoadData()
Me.DBSource.Tables("Company").LoadDataRow(dr.ItemArray, True)
Me.DBSource.Tables("Company").EndLoadData()

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.


ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of npond
npond

ASKER

TheLearnedOne,

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?

Nathan
Avatar of npond

ASKER

karthikeyanTP,

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

ASKER

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.

-Nathan