Duplicate Validation in code(vb)

How can I check for a record that exists before insert in code behind.

eg

In the below I want to check that the @QUALSCHEMEID exist or not

 Public Sub InsertGCH()

            'If Session("ModeType") = "Insert" Then

            Dim strConnString As String = ConfigurationManager.ConnectionStrings("IrisConnectionString").ConnectionString

            Dim con As New SqlConnection(strConnString)

            Dim cmd As New SqlCommand()

            cmd.CommandType = CommandType.StoredProcedure

            cmd.CommandText = "Insert_REVIEWS_GCH"

            'cmd.Parameters.Add("@TRAINEEID", SqlDbType.NVarChar).Value = Request("TRAINEEID")

            'cmd.Parameters.Add("@POT", SqlDbType.Int).Value = Request("POT")

            cmd.Parameters.Add("@QUALSCHEMEID", SqlDbType.Int).Value = ddlQUALSCHEMEID.SelectedValue.Trim()

            cmd.Parameters.Add("@ReviewID", SqlDbType.Int).Value = Session("REVIEWS")

            cmd.Parameters.Add("@GCH", SqlDbType.NVarChar).Value = txtGCH.Text.Trim()


            cmd.Connection = con



            con.Open()

            cmd.ExecuteNonQuery()




            con.Close()

            con.Dispose()





        End Sub

Open in new window

EdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
You either...

1 - modify Insert_REVIEWS_GCH so that before the insert it does something like:
IF NOT EXISTS (SELECT * FROM table WHERE QUALSCHEMEID=@QUALSCHEMEID)
       INSERT .....

Open in new window


2 - first execute a statement which returns whether or not the value exists, and then only run your insert code if it's "safe"
Dim NumberOfExistingRecords As Integer = 0
Using cmd As New SqlCommand("SELECT COUNT(*) FROM table WHERE QUALSCHEMEID=@QUALSCHEMEID", cnn)
    NumberOfExistingRecords = Convert.ToInt32(cmd.ExecuteScalar())
End Using

If NumberOfExistingRecords = 0 Then

    --your insert code goes here

End if

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EdAuthor Commented:
Perfect, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.