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("Compan y").NewRow ()
dr.Item("CompanyId") = CompanyId
dr.Item("CompanyName") = CompanyName
Me.DBSource.Tables("Compan y").BeginL oadData()
Me.DBSource.Tables("Compan y").LoadDa taRow(dr.I temArray, True)
Me.DBSource.Tables("Compan y").EndLoa dData()
And the error I get on EndLoadData is:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------
An unhandled exception of type 'System.Data.ConstraintExc eption' 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.
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("Compan
dr.Item("CompanyId") = CompanyId
dr.Item("CompanyName") = CompanyName
Me.DBSource.Tables("Compan
Me.DBSource.Tables("Compan
Me.DBSource.Tables("Compan
And the error I get on EndLoadData is:
--------------------------
An unhandled exception of type 'System.Data.ConstraintExc
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
karthikeyanTP,
Didn't work, got the same error on EndLoadData()
Didn't work, got the same error on EndLoadData()
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
I split points since you guys were nice enough to respond.
-Nathan
ASKER
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