Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...