dinartech dinartech - 2 months ago 8
MySQL Question

How to check for a duplicate name in a MySQL database?

How can you check for a duplicate name in a MySQL? In this instance, for example, this product should not be the same.

Dim SDA As New MySqlDataAdapter
Dim dbSource As New BindingSource

Try
cn.Open()
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert purchase.category set id_cat='" & txtID.Text & "', cat_product='" & txtNama.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Succes")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Dispose()
End Try

Load_AddCatPro()
txtID.Clear()
txtNama.Clear()

End Sub


form

Answer

I have solution for this problem , check this out.

    Try
        If txtNama.Text = "" Then
            MessageBox.Show("Isi Category Product", "Warning",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
            With txtID
                .Focus()
                .SelectAll()
            End With
            Exit Sub                    ' tell it to skip the rest
        End If

        cn.Open()
        cmd.Connection = cn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "select * from purchase.category where cat_product= '" & txtNama.Text & "'"
        rd = cmd.ExecuteReader
        If rd.HasRows Then
            MsgBox("Duplicate Entry Found", MsgBoxStyle.Critical)
            cn.Close()
        Else
            cn.Close()
            cn.Open()
            cmd.Connection = cn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "insert purchase.category set id_cat='" & txtID.Text & "', cat_product='" & txtNama.Text & "'"
            cmd.ExecuteNonQuery()
            MessageBox.Show("Data Tersimpan")
            'frmCustomer.DataGridViewCust.Refresh()
            cn.Close()
            Load_AddCatPro()
            txtID.Clear()
            txtNama.Clear()
        End If

enter image description here

Comments