sql server - "Violation of PRIMARY KEY constraint 'PK_Vehicle_Transactions'. Cannot insert duplicate key in object 'dbo.Vehicle_Transactions" -


there's webservice api design, each time push data cross webservice in return mov = "violation of primary key constraint 'pk_vehicle_transactions'. cannot insert duplicate key in object 'dbo.vehicle_transactions'. statement has been terminated." api doesn't know stopped , continue! kindly see source code below thanks

public sub uploadvehicle_transaction()         try             'do sync indacator proper upload in action             dim vt new datatable             vt = new statn_sync.datasettableadapters.vehicle_transactionstableadapter().getdata()          each dr datarow in vt.rows             dim icount integer = 0             dim mov string = comt.insertvehicle_transaction(convert.toint64(dr("transactionid")), _                                                                convert.todatetime(dr("transaction_date")), _                                                                convert.toint32(dr("bank")), _                                                                convert.tostring(dr("teller_number")), _                                                                convert.toint32(dr("amount")), _                                                                convert.tostring(dr("generated_by")), _                                                                convert.tostring(dr("station")), _                                                                convert.tostring(dr("customer_name")), _                                                                convert.toint32(dr("transaction_category")), _                                                                convert.tostring(dr("deposit_slip")), _                                                                convert.toint32(dr("sync")), _                                                                convert.todecimal(dr("penalty")), _                                                                convert.todecimal(dr("ogsg")), _                                                                convert.todecimal(dr("cmr")), _                                                                convert.todecimal(dr("goshen")), _                                                                convert.todecimal(dr("insurance")), _                                                                convert.todecimal(dr("ocost")), _                                                                convert.todecimal(dr("ogsg_renewal")), _                                                                convert.todecimal(dr("de_pulse")))              icount += 1             label1.text = " auto sync: " & icount             'update record             dim pls string = dr("transactionid").tostring              if (pls mov)                 addtolog((convert.tostring(": transferred") & mov.tostring() & text) + environment.newline)                 vta.updatetrans(dr("transactionid"))             end if         next      catch ex exception         addtolog(ex.message.tostring)     end try end sub 

the exception says it: violation of primary key constraint 'pk_vehicle_transactions'. table contains row primary key (transactionid) given. primary key unique throughout table.

there several solutions problem:

1) calculate latest transactionid

vt = new statn_sync.datasettableadapters.vehicle_transactionstableadapter().getdata()  //use query select max value of transactionid (something like) dim maxpk long =  'select max(transactionid) dbo.vehicle_transactions'    //increase maxpk 1 avoid duplicate key  maxpk = maxpk + 1     each dr datarow in vt.rows    dim icount integer = 0     //use our variable in insert    dim mov string = comt.insertvehicle_transaction((maxpk  + icount), _                        convert.todatetime(dr("transaction_date")), _ 

2) use auto increment on transactionid of dbo.vehicle_transactions

for refer following post: auto increment .this post made management studio of mssql 2012. same logic applies earlier version (2008,2005)

other solutions might found throughout stackoverflow

if can of further assistance, don't hesitate give me sign!

note: if previous data of no use you, can clear table prior insert using query: delete dbo.vehicle_transactionsthis query removes rows table. though have wary forgein keys might cause dataloss/exceptions.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -