i'm working on a voting system but im having problems in adding records... i have two a primary key, the idnumber and the address. the address is where I base if that certain address already voted. but the problem is everytime i try to add a record with the same address (ex. Block 1 lot 4), the data is duplicated which is not output i want. the program should detect that there is already an existing address. i hope you guys can help me.

this is the code i am using

Private Sub AddNewVoter()
        Dim li As New ListViewItem

        SqlString = "INSERT INTO voter (voterIDno, voterPass, voterLname, voterFname, voterMname, address, contactNo, gender, birthday, civilStatus, voterStatus) VALUES (" & _
                    "'" & txtidnumber.Text & "'," & _
                    "'" & txtpassword.Text & "'," & _
                    "'" & txtlname.Text & "'," & _
                    "'" & txtfname.Text & "'," & _
                    "'" & txtmi.Text & "'," & _
                    "'" & txtaddress.Text & "'," & _
                    "'" & txtcontactno.Text & "'," & _
                    "'" & cmbgender.Text & "'," & _
                    "'" & txtbirthday.Text & "'," & _
                    "'" & cmbcivilstatus.Text & "'," & _
                    "'" & cmbvotingstatus.Text & "')"

        Using conn As New MySqlConnection(ConnString)

            Using cmd As New MySqlCommand(SqlString, conn)

                cmd.CommandType = CommandType.Text

                cmd.Parameters.AddWithValue("voterIDno", txtidnumber.Text)

                cmd.Parameters.AddWithValue("voterPass", txtpassword.Text)

                cmd.Parameters.AddWithValue("voterLname", txtlname.Text)

                cmd.Parameters.AddWithValue("voterFname", txtfname.Text)

                cmd.Parameters.AddWithValue("voterMname", txtmi.Text)

                cmd.Parameters.AddWithValue("address", txtaddress.Text)

                cmd.Parameters.AddWithValue("contactNo", txtcontactno.Text)

                cmd.Parameters.AddWithValue("gender", cmbgender.Text)

                cmd.Parameters.AddWithValue("birthday", txtbirthday.Text)

                cmd.Parameters.AddWithValue("civilStatus", cmbcivilstatus.Text)

                cmd.Parameters.AddWithValue("voterStatus", cmbvotingstatus.Text)

                conn.Open()

                cmd.ExecuteNonQuery()

            End Using

        End Using

        MsgBox("New voter has been succesfully saved", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "SAVED")

    End Sub

Recommended Answers

All 3 Replies

It might have to do with your database design. You can always put a few columns in a table as "unique", like "VoterID","CandidateID" as unique, so that the Candidate of an area / a post can receive many votes from many voters (Because a voter 1 votes for Candidate A and Voter 2 votes for Candidate A is still considered unique. At the same time, for a priority based voting or whatever is called when voters are asked to select the candidate by preference 1 to X, you should put a Preference number as unique instead of Candidate so that Voter A votes for Candidate A as First choice and Voter A votes for Candidate B as second choice are considered as unique.)

Being unique is not always necessary to be primary key, but primary key requires to be unique.


Back to your question, you meant on exact address being duplicated (Like 2 address of: Block 1, Unit 1 which should not be the case, but Block 1, Unit 2 is allowed), then you should put the Address to be unique as well (Because 2 primary keys = when one of the 2 values are different, the address record will be allowed to be duplicated as quoted example on top). By the way, I dont think you should do this approach as one can have many eligible voters living in the same address. (Read: A married couple living in the same address, where both have the rights to vote.)

i have this code that will delete any duplicate entries in a list box,, hope this helps,
the list box has to be set to organize from a to z

Dim i, j As Integer
        Dim Arr As New ArrayList
        Dim ItemFound As Boolean

        'this outside loop goes through all the items in listbox
        For i = 0 To ListBox23.Items.Count - 1
            ItemFound = False

            'this inside loop compares the item in outside loop to items above it.
            'first item is skipped because we know it would be a match.
            For j = 0 To i - 1
                If ListBox23.Items.Item(i).trim = ListBox23.Items.Item(j).trim Then
                    ItemFound = True
                    Exit For
                End If
            Next j

            'if it was not found, it must be unique, add it to the array
            If Not ItemFound Then
                Arr.Add(ListBox23.Items.Item(i))
                ' ListBox24.Items.Add(i)
            End If
        Next i

        'clear the list box
        ListBox23.Items.Clear()

        're-populate the list box with the listarray
        ListBox23.Items.AddRange(Arr.ToArray)

        'cleanup
        Arr = Nothing

and this code will company one listbox with another and delete the names found
listbox23 is the main list

listbox 34 contains the item you want to see if its in 23,, if it is,, it will delete it out,, you can taylor the code to either say its found,, and then move on,, etc,,

Dim ii As Integer = 0
        Dim str As String

        For i = 0 To ListBox34.Items.Count - 1
            str = ListBox34.Items(ii).ToString()

            For Each Itm As String In ListBox23.Items

                If Itm.Contains(str) Then
                    ListBox23.Items.Remove(Itm)
                    Exit For
                End If

            Next
        Next
        MsgBox("Done!")
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.