DB Insert via dataadapter

garyfis

Member
Joined
Dec 18, 2003
Messages
5
Location
NC
I am having a problem with this code only when I am trying to insert into a SQL DB when the last record in the DB has been deleted. I get an error stating that PK 5 already exists, when I am trying to insert 10 rows. This also happens when the DB is clean.

Any help would be appreciated.

Dim newRow As DataRow
Dim X As Integer
Try
For X = 1 To spQuestions.DataRowCnt
newRow = dsInitialObservations.tblScreeningInitial.NewRow
newRow("BookingID") = _BookingID
newRow("NameID") = _NameID
newRow("ObservationSequence") = X
spQuestions.Col = 1
spQuestions.Row = X
newRow("Observation") = spQuestions.Value
spQuestions.Col = 2
spQuestions.Row = X
If spQuestions.Value = "1" Then
newRow("Answer") = 1
Else
newRow("Answer") = 0
End If
spQuestions.Col = 4
spQuestions.Row = X
newRow("Explanation") = IfEmpty(spQuestions.Value)
newRow("CreatedAt") = Now
newRow("CreatedBy") = strUserName
dsInitialObservations.tblScreeningInitial.Rows.Add(newRow)
Next
daInitialObservations.Update(dsInitialObservations, "tblScreeningInitial")
dsInitialObservations.AcceptChanges()
Catch ex As Exception
MessageBox.Show("Error - " & Err.Description, "Error Saving Initial Observations", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
 
If BookingID is set up as an Identity column you shouldnt be setting its value in code, just delete the line of code where you set that, and let SQL Server handle setting its value.
 
Back
Top