I m making registration form for teachers
in which teach ID is one coloum name
i want message box error if id number already exist in side the table
means uniqe ID

e.g if id no 1 is already exist
then 1 should not be added when next time user register the new teacher
with id no 1 with msg box

this is my coding

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim con As New SqlConnection("Data Source=.\DHEERAJ;AttachDbFilename=D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\schools.mdf;Integrated Security=True;User Instance=false;initial catalog=schools")

        If txtteachid.Text = "" Then
            MsgBox("teachers id is required field!")

        ElseIf txtfristname.Text = "" Then
            MsgBox("first name is required field!")
        ElseIf txtlastname.Text = "" Then
            MsgBox("last name is required field !")
        ElseIf txtphoneno.Text = "" Then
            MsgBox("phone no is required field!")
        ElseIf txtmoblie.Text = "" Then
            MsgBox("mobile no is required field!")
        ElseIf txtemail.Text = "" Then
            MsgBox("email id is required field! ")
        ElseIf txtqualification.Text = "" Then
            MsgBox("first name is required field!")


            Dim cmd As New SqlCommand("insert into teacher values(@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10)", con)

            Dim dr As SqlDataReader = cmd.ExecuteReader
            cmd.Parameters.Add("p1", SqlDbType.VarChar).Value = txtteachid.Text
            cmd.Parameters.Add("p2", SqlDbType.VarChar).Value = txtfristname.Text
            cmd.Parameters.Add("p3", SqlDbType.VarChar).Value = txtlastname.Text
            cmd.Parameters.Add("p4", SqlDbType.VarChar).Value = txtphoneno.Text
            cmd.Parameters.Add("p5", SqlDbType.VarChar).Value = txtmoblie.Text
            cmd.Parameters.Add("p6", SqlDbType.VarChar).Value = txtemail.Text
            cmd.Parameters.Add("p7", SqlDbType.VarChar).Value = cmbmarital.Text
            cmd.Parameters.Add("p9", SqlDbType.VarChar).Value = txtqualification.Text
            cmd.Parameters.Add("p8", SqlDbType.VarChar).Value = cmbgender.Text
            cmd.Parameters.Add("p10", SqlDbType.VarChar).Value = DateTimePicker1.Text

        End If
    End Sub

Edited 5 Years Ago by __avd: Added [code] tags.

Use IF EXISTS method in sql
if EXISTS returns true then rerun -1 or boolean as false... then give the message box appropriately...

This question has already been answered. Start a new discussion instead.