Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was asked by my boss to create an Application that will store new Applicants (Hirers) data. I have been going around in circles trying to create stored procedures or a TableAdapter which will not allow duplicate entries to be entered in the SQL DB.

Here is some of the code SQL Code:

SQL
<removed stored="" procedure="">
ALTER PROCEDURE dbo.DuplicateProcParam(@clmnFN nvarchar(200), @clmnPN nvarchar(50), @clmReturnDuplicateValue INT OUTPUT)

AS

    /*DECLARE @rc int */

    SELECT        clmnID, clmnFN, clmnPN, clmnAPN, clmnRB, clmnRP, clmnTA, clmnAsignTo
    FROM            tblAddResume
    WHERE        (clmnFN = @clmnFN) AND (clmnPN = @clmnPN)

    if @@ROWCOUNT=1
        Begin
            SELECT @clmReturnDuplicateValue=1
            RETURN @clmReturnDuplicateValue
        END
    Else
        Begin
            SELECT @clmReturnDuplicateValue=0
            RETURN @clmReturnDuplicateValue
        End


I have a Class which I pass the values to.
VB
'Checks for Duplicate Users
    Public Function DuplicateProcedure(ByVal FN As String, ByVal PN As String)
        Dim AR As New NSCP.ResumeClass           'Properties

        Dim conn As New SqlConnection()           
        Dim txtValPass As New frmResume
        conn.ConnectionString = strCadenaConexion

        ' AR.propFN = txtValPass.txtFullName.Text
        'AR.propPN = txtValPass.txtPhoneNumber.Text

        Dim cmd As New SqlCommand()
        cmd.Connection = conn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "DuplicateProcParam"



        cmd.Parameters.AddWithValue("@clmnFN", FN)
        cmd.Parameters.AddWithValue("@clmnPN", PN)
        cmd.Parameters("@clmReturnDuplicateValue").Direction = ParameterDirection.Output
        cmd.ExecuteScalar()

        MsgBox(cmd.Parameters.Item("@clmReturnDuplicateValue").Value.ToString)
        cmd.Parameters.Clear()
        ' Create a SqlParameter for each parameter in the stored procedure.
        Dim InsertResumeProcedure As New SqlParameter
        InsertResumeProcedure = cmd.Parameters.Add("@clmReturnDuplicateValue", SqlDbType.Int)
        InsertResumeProcedure.Direction = ParameterDirection.ReturnValue




        Dim previousConnectionState As ConnectionState
        previousConnectionState = conn.State

        Try
            If conn.State = ConnectionState.Closed Then
                conn.Open()

                Dim jj = cmd.Parameters("@clmReturnDuplicateValue").Value

                If jj <> 0 Then
                    'if found show 0 else 1
                    dtrEst = cmdEst.ExecuteReader
                    dtrEst.Read()
                    AR.propID = dtrEst.Item("clmnID")
                    AR.propFN = dtrEst.Item("clmnFN")
                    AR.propPN = dtrEst.Item("clmnPN")
                    AR.propAPN = dtrEst.Item("clmnAPN")
                    AR.propRB = dtrEst.Item("clmnRB")
                    AR.propRP = dtrEst.Item("clmnRP")
                    AR.propTA = dtrEst.Item("clmnTA")
                    AR.propAT = dtrEst.Item("clmnAsignTo")
                End If
            Else
                AR.propID = Nothing
                AR.propFN = Nothing
                AR.propPN = Nothing
                AR.propAPN = Nothing
                AR.propRB = Nothing
                AR.propRP = Nothing
                AR.propTA = 1
                AR.propAT = Nothing
            End If

        Finally
            If previousConnectionState = ConnectionState.Closed Then
                conn.Close()
            End If
        End Try
        Return AR
    End Function



Finally it goes to my main form
VB
Private Sub btnSaveEditDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveEditDelete.Click
        If Me.btnSaveEditDelete.Text = "Save" Then
            If txtFullName.Text = "" Then
                FullNameErrorProvider.SetError(txtFullName, "Required Field")
            ElseIf txtPhoneNumber.Text = "" Then
                PhoneNumberErrorProvider.SetError(txtPhoneNumber, "Required Field")
            ElseIf txtResumePath.Text = "" Then
                ResumePathErrorProvider.SetError(txtResumePath, "Required Field")
            Else

                '   AR.propFN = txtFullName.Text
                '  AR.propPN = txtPhoneNumber.Text



                objConn.openConexion()
                AR = objConn.DuplicateProcedure(txtFullName.Text, txtPhoneNumber.Text)
                objConn.closeConexion()
                If AR.propFN = txtFullName.Text And AR.propPN = txtPhoneNumber.Text Then
                    If MsgBox("This Applicant is already in the Data Base! Do you wish to Edit the Applicants Information?" & _
                    vbCrLf & "ID: " & AR.propID & vbCrLf & "Full Name: " & AR.propFN & _
                    vbCrLf & "Alt Phone Number: " & AR.propAPN & vbCrLf & "Recommended By: " & AR.propRB & _
                    vbCrLf & "Resume Path: " & AR.propRP & vbCrLf & "Times Applied: " & AR.propTA & _
                    vbCrLf & "Assigned To: " & AR.propAT, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        Call ENRValues(AR)
                        Me.Close()
                    ElseIf AR.propFN <> txtFullName.Text And AR.propPN <> txtPhoneNumber.Text Then
                        Call ARValueAssignation()
                        objConn.InsertResumeProcedure(AR)
                        MsgBox("The Applicants information was successfully stored.", MsgBoxStyle.Information, "Sucessfull Storage")
                        Me.Dispose()
                    Else
                        MsgBox("Duplicate Entries are not allowed on the Data Base", MsgBoxStyle.Information, "Data Rejection")
                    End If
                Else
                    MsgBox("Store Data")
                End If
            End If
        ElseIf Me.btnSaveEditDelete.Text = "Update" Then

        End If

    End Sub



The dead line for this app is soon. Please help!!!!

[Modified: fixed duplicate pre tags to fix the formatting.]
Posted
Updated 21-Apr-10 11:04am
v2

So, tell us what is and what isn't working.

I do have a few comments and questions. First, as a matter of memory usage, you create a New SqlParameter and then immediately set it to something else at:
VB
Dim InsertResumeProcedure As New SqlParameter
InsertResumeProcedure = cmd.Parameters.Add("@clmReturnDuplicateValue", SqlDbType.Int)

You don't need to have the New keyword. That's actually creating a new SqlParameter in memory that never gets used.

Secondly, I'm quite confused by your DuplicateProcedure. In your main form, you tell your objConn...whatever that is to open the connection. Then, you call DuplicateProcedure. So, you get into DuplicateProcedure and then you create a brand new connection. Then, you run your stored procedure and the only thing you do with the result is send it to a MessageBox. Then, you clear the parameters.
After that, you create InsertResumeProcedure which you then never use. Then, you get into your Try/Catch block. First, you check to see if the connection is open...which it will never be because you just created it in this method and never opened it. So, there's no reason to check that. Then, you open it, and I guess you think that magically the parameters that you just cleared will still be there. And what happened to naming conventions? "jj" to represent whether or not a value is a duplicate?
And even if the connection somehow did magically open on its own when you checked its connection state, why would you ever set the properties to Nothing? You return AR, and if somehow you got to your section of code where you set the values to Nothing, the in your main form, you then check to see if propFN equals something, which will throw an error because you just set propFN to Nothing.

So, since "jj" should never get beyond being null (because you cleared the parameters and never reloaded them), AR will always be a new object with nothing in it.

You have all kinds of issues in your code. Maybe you need to diagram the steps and the objects that you're using and actually see what's happening to them, or what should happen to them.
 
Share this answer
 
Add a unique key constraint to the tblAddResume table on the clmnFN and clmnPN columns.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900