Visual Basic .NET » Database Programming
Violation of PRIMARY KEY constraint 'PK Employee'. Cannot insert duplicate key in object 'dbo.Emp'. The statement has been terminated -- anu --



Hi!!
This is my vb.net code behind application to add a new record to the Emp table with Primary key EmpId and Stored procedure spEmpAdd as follows:
spEmpAdd:

CREATE PROCEDURE


@EmpId as int,

@EmpName as nchar(10),

@Empsal as nchar(10)

AS

BEGIN

insert into Emp(EmpId,EmpName,Empsal) values(@EmpId,@EmpName,@Empsal)

END

code for btnadd_click():

------------------------------------------

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

Dim con As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

cleartext()

con.Open()

Page.RegisterStartupScript("SetFocus",
"")

Dim cmd As New SqlCommand("spEmpAdd", con)

cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(New SqlParameter("@EmpId", SqlDbType.Int, 4))

cmd.Parameters("@EmpId").Direction = ParameterDirection.Input

cmd.Parameters("@EmpId").Value = Val(txtEmpId.Text)

cmd.Parameters.Add(New SqlParameter("@EmpName", SqlDbType.NChar, 10))

cmd.Parameters("@EmpName").Direction = ParameterDirection.Input

cmd.Parameters("@EmpName").Value = txtEmpName.Text.ToString

cmd.Parameters.Add(New SqlParameter("@Empsal", SqlDbType.NChar, 10))

cmd.Parameters("@Empsal").Direction = ParameterDirection.Input

cmd.Parameters("@Empsal").Value = txtEmpsal.Text.ToString

cmd.ExecuteNonQuery()

con.Close()

Page.RegisterClientScriptBlock("ALERT", "")

Dim i As Integer = Val(txtEmpId.Text)

'cleartext()

filldgadd(i)

End Sub

but Iam getting the following error while running this application:

Violation of PRIMARY KEY constraint 'PK_Employee'. Cannot insert duplicate key in object 'dbo.Emp'. The statement has been terminated

plz help me in solving this problem

regards Anupama

[Submit Comment]Home