ASP.Net Error: Violating the Primary Key constraint. can not insert the duplicate key into the object

Last Reply one month ago By dharmendr

Posted one month ago

hi, 

can you help me to take a look of my code why i got an error of the duplicate key.  thanks. 

        DBConnection.Open()
        DBCommand = New SqlCommand("QStudentCWIDCheck_sp", DBConnection)
        DBCommand.Parameters.AddWithValue("@StudentCWIDCheck", StudCWIDtxt.Text.ToString())
        DBCommand.CommandType = CommandType.StoredProcedure
        DBReader = DBCommand.ExecuteReader()

        'If DBReader.HasRows Then
        If DBReader.Read() Then
            If StudCWIDtxt.Text = DBReader("StudCWID") Then
                'lblerror.Visible = True
                'Exit Sub

                DBConnection.Close()
                DBConnection.Open()
                DBCommand = New SqlCommand("QStudentRecordUpdateFieldwork_sp", DBConnection)
                DBCommand.CommandType = CommandType.StoredProcedure

                DBCommand.Parameters.Add("@StudentCWID", SqlDbType.NVarChar, 9).Value = StudCWIDtxt.Text
                DBCommand.Parameters.Add("@StudentTermID", SqlDbType.NVarChar).Value = SemTerm
                DBCommand.Parameters.Add("@StudentYearID", SqlDbType.NVarChar).Value = SemYear
                DBCommand.Parameters.Add("@StudentInstructorName", SqlDbType.NVarChar, 50).Value = FacID.Text
                DBCommand.Parameters.Add("@StudentOPAddress", SqlDbType.NVarChar, 225).Value = Addresslbl.Text
                DBCommand.Parameters.Add("@StudentOPPhone", SqlDbType.NVarChar, 50).Value = OPPhonelbl.Text
                DBCommand.Parameters.Add("@StudentSupervisorName", SqlDbType.NVarChar, 50).Value = Supervisorlbl.Text
                DBCommand.Parameters.Add("@StudentFWCourse", SqlDbType.NVarChar, 50).Value = DropDownList1.SelectedValue
                DBCommand.Parameters.Add("@StudentOrgCode", SqlDbType.NVarChar, 150).Value = OrgCodelbl.Text
                DBCommand.Parameters.Add("@StudentOPTitle", SqlDbType.NVarChar, 100).Value = ProgCodelbl.Text

                DBCommand.ExecuteNonQuery()
                DBConnection.Close()
                'End If
            End If
        Else
            DBConnection.Close()
            DBConnection.Open()
            DBCommand = New SqlCommand("QStudentRecordAdd_sp", DBConnection)
            DBCommand.CommandType = CommandType.StoredProcedure

            DBCommand.Parameters.Add("@StudentCWID", SqlDbType.NVarChar, 9).Value = StudCWIDtxt.Text
            DBCommand.Parameters.Add("@StudentOrgCode", SqlDbType.NVarChar, 150).Value = OrgCodelbl.Text
            DBCommand.Parameters.Add("@StudentOPTitle", SqlDbType.NVarChar, 100).Value = ProgCodelbl.Text
            DBCommand.Parameters.Add("@StudentOPAddress", SqlDbType.NVarChar, 225).Value = Addresslbl.Text
            DBCommand.Parameters.Add("@StudentOPPhone", SqlDbType.NVarChar, 50).Value = OPPhonelbl.Text
            DBCommand.Parameters.Add("@StudentSupervisorName", SqlDbType.NVarChar, 50).Value = Supervisorlbl.Text
            DBCommand.Parameters.Add("@StudentUpdate", SqlDbType.NVarChar, 50).Value = Datetxt.Text
            DBCommand.Parameters.Add("@StudentFirstName", SqlDbType.NVarChar, 50).Value = StudFNametxt.Text
            DBCommand.Parameters.Add("@StudentLastName", SqlDbType.NVarChar, 50).Value = StudLNametxt.Text
            DBCommand.Parameters.Add("@StudentCPhone", SqlDbType.NVarChar, 14).Value = CPhonetxt.Text
            DBCommand.Parameters.Add("@StudentTermID", SqlDbType.NVarChar).Value = SemTerm
            DBCommand.Parameters.Add("@StudentYearID", SqlDbType.NVarChar).Value = SemYear
            DBCommand.Parameters.Add("@StudentAddress", SqlDbType.NVarChar, 75).Value = StAddress.Text
            DBCommand.Parameters.Add("@StudentCity", SqlDbType.NVarChar, 30).Value = City.Text
            DBCommand.Parameters.Add("@StudentZip", SqlDbType.NVarChar, 20).Value = ZipCode.Text
            DBCommand.Parameters.Add("@StudentEmail", SqlDbType.NVarChar, 225).Value = Email.Text
            DBCommand.Parameters.Add("@StudentInstructorName", SqlDbType.NVarChar, 50).Value = FacID.Text
            DBCommand.Parameters.Add("@StudentFWCourse", SqlDbType.NVarChar, 50).Value = DropDownList1.SelectedValue

            DBCommand.ExecuteNonQuery()
            DBConnection.Close()
        End If

here is the error.

Violating the Primary Key constraint.  can not insert the duplicate key into the object dbo_Students. and termiate 

Posted one month ago

Hi phonghue,

You are trying to insert a duplicate value in dbo_Students table.

You can set auto increment for the column and then there should never be a problem with duplicating the value, the database will take care of that.

If you need to specify a value yourself, you'll need to write code to determine what the max value for that field is and then increment that to avoid the duplication.