SqlException Violation of PRIMARY KEY constraint error

C

Chris Clement

I am trying to update a record in a SQL database through a web service but
am getting this error:

System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint
'PK_Items'. Cannot insert duplicate key in object 'VideoProposalItems'.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
at proposalWebService.Service2.setVideoProposalItemsData(String strPass,
Byte[] compressedData) in
c:\inetpub\wwwroot\proposalwebservice\service2.asmx.cs:line 757

line 757: sqlDataAdapter2.Update(DataSet1,"Items");

This record already exists but for some reason the Update method is trying
to insert a new record rather than update the current one. What makes the
Update method decide whether to use insert or update?

Chris
 
J

Jon Skeet [C# MVP]

Chris Clement said:
I am trying to update a record in a SQL database through a web service but
am getting this error:

System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint
'PK_Items'. Cannot insert duplicate key in object 'VideoProposalItems'.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
at proposalWebService.Service2.setVideoProposalItemsData(String strPass,
Byte[] compressedData) in
c:\inetpub\wwwroot\proposalwebservice\service2.asmx.cs:line 757

line 757: sqlDataAdapter2.Update(DataSet1,"Items");

This record already exists but for some reason the Update method is trying
to insert a new record rather than update the current one. What makes the
Update method decide whether to use insert or update?

It depends on what the state of the DataRow is - if it's Modified, it
uses the update. If it's Added, it uses the insert command.
 
C

Chris Clement

Thanks, Greg and Jon. Found my problem.


Greg said:
RowState is examined to determine which command to use.


Chris Clement said:
I am trying to update a record in a SQL database through a web service but
am getting this error:

System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint
'PK_Items'. Cannot insert duplicate key in object 'VideoProposalItems'.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
at proposalWebService.Service2.setVideoProposalItemsData(String strPass,
Byte[] compressedData) in
c:\inetpub\wwwroot\proposalwebservice\service2.asmx.cs:line 757

line 757: sqlDataAdapter2.Update(DataSet1,"Items");

This record already exists but for some reason the Update method is trying
to insert a new record rather than update the current one. What makes the
Update method decide whether to use insert or update?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top