Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have an error saying
Additional information: Violation of PRIMARY KEY constraint 'PK__tblPayme__A17005CC944C102D'. Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).
.

What does it mean? How can I fix it? Thanks

What I have tried:

VB
<pre>Imports System.Data.SqlClient
Imports System.Data

Public Class Payment
    Dim conn As New SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim strcon As String = "Server=DESKTOP-C6IEOUN\SQLEXPRESS;database =NEWCMO; integrated security=True;"
    Private Sub dtpDateToday_ValueChanged(sender As Object, e As EventArgs) Handles dtpDateToday.ValueChanged

    End Sub

    Private Sub Payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
        txtBal.Text = Val(txtAmt.Text) - Val(txtPayment.Text)
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim cmd As New SqlCommand
        conn.ConnectionString = strcon
        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = "INSERT INTO tblPayment (patientID,Surname,Firstname,Age,[Total Amount to be Paid],[Type of Payment],Payment,Balance) VALUES(@PID,@SN,@FN,@Age,@Amt,@TypePayment,@Payment,@Balance)"

        cmd.Parameters.AddWithValue("@PID", txtPID.Text)
        cmd.Parameters.AddWithValue("@SN", txtSN.Text)
        cmd.Parameters.AddWithValue("@FN", txtFN.Text)
        cmd.Parameters.AddWithValue("@Age", txtAge.Text)
        cmd.Parameters.AddWithValue("@Amt", txtAmt.Text)
        cmd.Parameters.AddWithValue("@TypePayment", cmbTypeOfPayment.Text)
        cmd.Parameters.AddWithValue("@Payment", txtPayment.Text)
        cmd.Parameters.AddWithValue("@Balance", txtBal.Text)

        cmd.ExecuteNonQuery()
        MessageBox.Show("Successful Added Data")
        conn.Dispose()
        conn.Close()
    End Sub

    Private Sub Payment_Click(sender As Object, e As EventArgs) Handles Me.Click
        ''''''''''''''''Searching''''''''''''''''''''''''''

        Dim connection As New SqlConnection("Server=DESKTOP-C6IEOUN\SQLEXPRESS;database =NEWCMO; integrated security=True;")
        Dim command As New SqlCommand("Select * From tblRegPatient where patientID =@PID", connection)

        ' command.Parameters.Add("@PID", SqlDbType.Int).Value = txtPID.Text
        command.Parameters.AddWithValue("@PID", txtPID.Text)
        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()
        adapter.Fill(table)
        If table.Rows.Count() > 0 Then

            txtSN.Text = table.Rows(0)(1).ToString()
            txtFN.Text = table.Rows(0)(2).ToString()
            txtAge.Text = table.Rows(0)(7).ToString()

        Else
            MsgBox("No Data Found", vbInformation, "Not Found")
        End If
    End Sub
End Class
Posted
Updated 2-Feb-17 17:36pm

The error message quite clearly tells you that you are trying to insert a duplicate value into the Payment table.

Have a look at this CodeProject article to get an understanding of indexing - Indexes in MS SQL Server[^]
 
Share this answer
 
The error message is pretty explicit:
Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).
You are trying to insert a row with a value that is already in the table, in the Primary Key column - probably patientID

If you want to change values, you don't use an INSERT - you use an UPDATE:
SQL
UPDATE MyTable SET MyColumn1 = @NewValue1, MyColumn2 = @NewValue2 WHERE MyPrimaryKey = @PID
 
Share this answer
 
Quote:
Violation of PRIMARY KEY constraint 'PK__tblPayme__A17005CC944C102D'. Cannot insert duplicate key in object 'dbo.tblPayment'. The duplicate key value is (1).

It means what it says.
You are trying to INSERT a new record with a PRIMARY KEY that is already in the database, and it is forbidden.
Quote:
How can I fix it?

Do not reuse the same value for the PRIMARY KEY.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900