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