garyfis Posted February 7, 2005 Posted February 7, 2005 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 Quote
Optikal Posted February 7, 2005 Posted February 7, 2005 If BookingID is set up as an Identity column you shouldn't be setting its value in code, just delete the line of code where you set that, and let SQL Server handle setting its value. Quote
garyfis Posted February 8, 2005 Author Posted February 8, 2005 The BookingID isn't the Identity Column. That is InitialObservationsID which is not being set. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.