Monday, March 26, 2012

duplicate primary key error

Im gettig the error "



Violation of PRIMARY KEY constraint 'PK_Employees'. "

for the following code..i cant seem to find the error in the code.


'data adapter

Dim dAdapt As New SqlClient.SqlDataAdapter

'create a command object

Dim objCommand As New SqlClient.SqlCommand

'command builder

Dim builder As SqlClient.SqlCommandBuilder

'connection string

Dim cnStr As String = "Data Source=ELEARN-FRM-BETA;Initial Catalog=StudentPlayGround;Integrated Security=True"

'connection

Dim cnObj As New SqlConnection(cnStr)

'dataset

Dim ds As DataSet

Dim courseID As Integer

Dim courseName As String

Dim desc As String

Dim maxT As Integer

Dim sql As StringBuilder

Dim cmdTxt As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

BindData()

End If

End Sub

Private Sub BindData()

connect()

DataBind()

End Sub

Private Sub connect()

'connection

objCommand.Connection = cnObj

'query string

Dim query As String = "SELECT * from StudentPlayground..Courses"

'setting the select command

dAdapt.SelectCommand = New SqlClient.SqlCommand(query, objCommand.Connection)

'dataset

ds = New DataSet("Course Listings")

cnObj.Open()

dAdapt.Fill(ds, "Courses")

cnObj.Close()

End Sub

Protected Sub submitButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitButton1.Click

courseID = CInt(CType(FindControl("TextBox1"), TextBox).Text)

courseName = CType(FindControl("TextBox2"), TextBox).Text

desc = CType(FindControl("Textbox3"), TextBox).Text

maxT = CInt(CType(FindControl("TextBox4"), TextBox).Text)

REM Build our parameterized insert statement

sql = New StringBuilder("INSERT INTO Courses ")

sql.Append("(CourseID,CourseName,Description,MaxTrainees)")

sql.Append("VALUES (@.courseID,@.courseName,@.desc,@.maxT)")

cmdTxt = sql.ToString

REM Add parameter values to command

REM Parameters used to protect DB from SQL injection attacks

With objCommand.Parameters

.Clear()

.AddWithValue("@.courseID", courseID)

.AddWithValue("@.courseName", courseName)

.AddWithValue("@.desc", desc)

.AddWithValue("@.maxT", maxT)

End With

objCommand.CommandText = cmdTxt

objCommand.Connection = cnObj

REM Now execute the statement

'connection

cnObj.Open()

objCommand.ExecuteNonQuery()

cnObj.Close()

objCommand.Dispose()

submitButton1.Text = "Record Added!"

End Sub

Well in that particular listing, I don't see any insert into an Employees table. Are you familiar with the PK_Employees primary key constraint in your schema? What table does it exist on (my guess is employees) and on what colum(s) is it on?

I would look at what is going on when the one insert shown above does occur. Are there any triggers entering info that violates this constraint?

If not, I would reanalyze and make sure this is really the section of code causing this problem.

|||checkout whick table has this primary key defined 'pk_employees' (if u r not aware , use the syskeys system table to get that info) . ur code may be adding (repeating)values to that table either directly or thru a trigger.....try to resolve it from there|||

My Apologies...the error is Violation of PRIMARY KEY constraint 'PK_Courses'. Cannot insert duplicate key in object 'dbo.Courses'. This is on the CourseID column in the table Courses. there r no triggers on this table.

aThe strange thing is even though this error appears..my record is still getting inserted correctly in the db. Also this is the ONLY code-beind code present for this page. I just left out the import statements and classdefinition in my post.

|||

The PK constraint error is thrown because of a duplicate "value" (Course) not key attempting to insert into the table.

You can sloppily add On Error Resume Next into your vb.net so you don't see the server side errors, or you check for dupes before you insert records.

Adamus

|||there are no duplicate courses inthe table..i try and add new unique courses each time and experience the error.|||

Do you have a concatenated key or one primary key?

I've experienced similar errors using concatenation and had to remove all constraints and add one key at a time, then one record at a time until I found the error.

Adamus

|||just one primary key on the id field. thats it. no triggers.|||

1. Remove the key - drop the constraint (sp_helpconstraint)
2. Relocate the records
3. Recreate the key
4. Re-import the records

When in doubt, resort to the basics.

Adamus

sql

No comments:

Post a Comment