Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting below error while updating the data adapter.
" Violation of PRIMARY KEY constraint 'PK__Compalint__62D5F22C'. Cannot insert duplicate key in object 'Compalint'. The statement has been terminated. "

i am updating table but why it is giving insertion error ?
Let me know if i am doing something wrong.. :(

i used below code:-

VB
Dim Da As New SqlDataAdapter("select userid,loginid from compalint", conn)
  Dim cmdbld As New SqlCommandBuilder(Da)
     cmdbld.GetUpdateCommand()
         Da.Update(ds1, "table")


and my ds1 contain two fields userid,loginid only...

thanks in advance...
Posted
Updated 10-Apr-19 2:09am
Comments
Jim Jos 4-Jun-12 4:56am    
What are you having in DS1? Please put the code where you are getting DS1 rows updated.. If you are putting new rows in DS1 it will not update it will only try to insert..
ssd_coolguy 4-Jun-12 5:03am    
thanks for reply...
dataset ds1 contains userid and loginid. and it is getting from xml file.
and i want to update compalint table's loginid field only.. because userid is primary key..
is my dataadapter update code is wrong?
Jim Jos 4-Jun-12 5:07am    
You need to update the value in dataset so that the 'RowState' property is set to UPDATE mode.. then only it will update.. that's y I am asking the code where you update the dataset. You are saying Dataset is loaded from xml but you are trying to update the table.. DS1 should be taken from Complaint table for update to work.. otherwise it will be inserting only..
ssd_coolguy 4-Jun-12 5:20am    
yeah Sir you are right we need to set the 'RowState' property is UPDATE mode..
As per you, we can't update the tabel if we not take dataset from that table only..
So is there any way to solve this problem??

Sorry for my poor english :(
Jim Jos 4-Jun-12 5:52am    
OK no problems.. let me summarize your requirements.. 1. Read Data from xml.. 2. update the complaint table using the data read from xml .. am I right?

Check the Update Command I think it wants to change or insert a PrimaryKey column. Improve the Update Command.
Or
You need to check the constraints of the table in SQL Server. Remove the unnecessarily constraints.
Or
After the Transact-SQL statement is first generated, the application must explicitly call RefreshSchema if it changes the statement in any way. Otherwise, the GetUpdateCommand will still be using information from the previous statement, which might not be correct. The Transact-SQL statements are first generated when the application calls either Update or GetUpdateCommand. (More information in MSDN)

And also, look at this: Generating Commands with CommandBuilders (ADO.NET)
 
Share this answer
 
v2
Comments
ssd_coolguy 4-Jun-12 5:12am    
thanks for reply..
what you mean improve the update command? i am not writing any updated command..
and my table contins only one primary key "uesrid".
Can you please explain more??
Shahin Khorshidnia 4-Jun-12 5:24am    
You're welcome. Please read the updated solution again :)
ssd_coolguy 4-Jun-12 6:04am    
hi shahin .. i am confused... :(

because.. when i seen the commandtext from cmdbld.GetUpdateCommand().commandtext() and i got below update command..

UPDATE [compalint] SET [userid] = @p1, [loginid] = @p2 WHERE (([userid] = @p3) AND ((@p4 = 1 AND [loginid] IS NULL) OR ([loginid] = @p5)))

update command is right.. but why it is giving error?? :( :(
Shahin Khorshidnia 4-Jun-12 7:35am    
Because you can not set userId in update command. UseID is a primary key and I'm sure it's an Identity (Means increasing automatically). generally you can not set Identity Primary Keys by hand.
ssd_coolguy 4-Jun-12 8:43am    
hey sorry but how i can remove it from update list... i tried to remove from select but it's giving error as primary key needs to update statement..:(
Here is a solution (lets use DataTable instead of DataSet)

1. Let dt1 have xml data

Create dt2 from DB

VB
Dim Da As New SqlDataAdapter("select userid,loginid from compalint", conn)
        Dim cmdbld As New SqlCommandBuilder(Da)
        Dim dt2 As DataTable = New DataTable
        Da.Fill(dt2)


3. Now you need to go through each row in dt1 and update dt2. Once you have finished updating dt2 call the udpate command..

VB
Dim foundRows As DataRow()

For Each dRow As DataRow In dt1.Rows

    'search userid in Dt2
    foundRows = dt2.Select("userid = '" & dRow("UserId") & "'")
    'go thru each row in foundrows and update the value
    For Each dFoundRow As DataRow In foundRows
        dFoundRow("LoginId") = dRow("LoginId")
    Next

Next

dt2.AcceptChanges()

cmdbld.GetUpdateCommand()
Da.Update(dt2, "table")
 
Share this answer
 
v2
Comments
ssd_coolguy 5-Jun-12 0:49am    
yes boss i think it will work..
but see my seanario..xml dataset conatins only 2 rows but another dataset contains many row..means do i need to make data relation between them?

i cleared xmldataset and added another dataset's datatable into it and tried but it giving same error..:(
Jim Jos 5-Jun-12 1:02am    
Ok wait I ll post a working solution..
ssd_coolguy 5-Jun-12 13:21pm    
hii..waiting for solution...:)
ssd_coolguy 6-Jun-12 6:10am    
Hey.. thank you very much...
your version 2 solution is working fine.. :) :D
Thanks..
You need to update the value in dataset so that the 'RowState' property is set to UPDATE mode.. then only it will update.. that's y I am asking the code where you update the dataset. You are saying Dataset is loaded from xml but you are trying to update the table.. DS1 should be taken from Complaint table for update to work.. otherwise it will be inserting only..
 
Share this answer
 
Comments
Dave Kreskowiak 10-Apr-19 11:38am    
And I doubt they're still looking for a solution to this SEVEN YEARS later.
OriginalGriff 11-Apr-19 8:18am    
See S&A ...
Dave Kreskowiak 11-Apr-19 8:24am    
Yeah, I already did. I posted the above and then found him in S&A when I checked an hour later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900