Reputation: 29
Platform : V4.0, SQL Server 2008, Windows 2003.
Below code throws SqlException on FillMethod
:
"Primary Key Violation Error. Insert to the Table Failed".
Not sure why in Fill Method. Its not happening every time. Column1 and Column2 - Composite Key.
Dim ObjDAdapter As SqlDataAdapter
String selectstring = "select * from Table where Column1 = 'A' and Column2 = 'B'"
ObjDAdapter = New SqlDataAdapter(selectString, myConnection)
ObjDAdapter.Fill(Dataset)
Upvotes: 1
Views: 692
Reputation: 663
I have similar code to fill a dataset with rows from an SQL query and it works fine. See if this helps any:
Dim ObjDAdapter As SqlDataAdapter
String selectstring = "select * from Table where Column1 = 'A' and Column2 = 'B'"
ObjDAdapter = New SqlDataAdapter(selectString, myConnection)
ObjDAdapter.AcceptChangesDuringFill = False
ObjDAdapter.Fill(Dataset)
The only change is setting AcceptChangesDuringFill to false.
But it sounds like maybe the local datatable you created has a different primary key setup than the SQL table.
I.e. if your SQL table has two fields setup as the primary key, but your local table only has one field setup as the key, you may get sporadic key violation errors when it tries to insert the rows into the local table.
Upvotes: 1