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!")
            Me.txtteachid.Focus()

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

        Else

            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

            con.Open()
            cmd.ExecuteNonQuery()
            MsgBox("added")
            con.Close()
        End If
    End Sub

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.