prevent duplicate entries to database
<p>I want to prevent duplicate entries to my inventory form using vb.net and MySQL as the database, here is my code:</p>

<pre><code> Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim myCommand As New MySqlCommand
    Dim conn As MySqlConnection
    Dim i As String
    conn = New MySqlConnection
    conn.ConnectionString = "server = localhost;username= root;password= a;database= secret"
    Try
        conn.Open()
    Catch mali As MySqlException
        MsgBox("connot establish connection")
    End Try

    Dim intReturn As Integer
    Dim strSql As String = " select * from personnel where pcode = @pcode"

    Dim sqlcmd As New MySqlCommand(strSql, conn)
    With sqlcmd.Parameters
        .AddWithValue("@pcode", CType(pcode.Text, String))
    End With

    intReturn = sqlcmd.ExecuteScalar

    If (intReturn &gt; 0) Then
        cmd = New MySqlCommand("Insert into personnel values('" &amp; pcode.Text &amp; "','" &amp; lname.Text &amp; "','" &amp; fname.Text &amp; "','" &amp; office.Text &amp; "','" &amp; designation.Text &amp; "')")
        i = cmd.ExecuteNonQuery


        If pcode.Text &lt;&gt; "" Then
        ElseIf i &gt; 0 Then
            MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
            mrClean()
            ListView1.Tag = ""
            Call objLocker(False)
            Call LVWloader()
            Call calldaw()
        Else
            MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
        End If
    Else
        MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")

    End If
</code></pre>

<p>end sub</p>

<p>i found this while i search for answer, but when i tried to run it, it does not read the insert command but rather it goes directly to the msbox "Personnel ID Already Exist" even if theres no thesame Personnel ID.</p>

<p>can someone check why it does not read the insert please,</p>

<p>my Database tables values:</p>

<p>pcode = primary key</p>

<p>lname = longtext</p>

<p>fname = longtext</p>

<p>office = longtext</p>

<p>designation = longtext</p>

<p>any help will be much appreciated, thanks,</p>