?
Solved

VB.Net - Comparison to figure out if there are duplicates

Posted on 2007-11-29
6
Medium Priority
?
156 Views
Last Modified: 2011-10-03
I have the following scenario.  I have a SQL Server table that holds PERSON data such as name, address, phone number etc..  The Email_ID is the Primary Key.  I have 6 kinds of Persons:

1.  Requestor           -   bobscott@yahoo.com  - strRQEmailID
2.  Project Leader    -   bobscott@yahoo.com - strPLEmailID
3.  Primary Contact  -   lindamathews@yahoo.com - strPMEmailID
4.  Admin Contact   -    ronsloan@yahoo.com - strADEmailID
5.  Technical Contact  -  shirleywhite@yahoo.com - strTCEmailID
6.  24HrSupport Contact - ronsloan@yahoo.com - strTWEmailID

The persons in 1 and 2 (don't have to be) but they can be the same person.
The persons in 3,4,5,6 can be the same person also.

I have 6 separate strings that hold the email-ids of each.    What I need to do is compare each email-id to the other and determine which ones are unique and which ones are duplicates.  Then I need to discard the duplicates and INSERT only the ones that are unique.  As I mentioned earlier, the EmailID is the Primary key.

Can someone please help with the logic of the code (in VB.Net).    I don't have a code snippet to attach because I don't know where to begin.
0
Comment
Question by:msyed1
  • 3
5 Comments
 
LVL 18

Accepted Solution

by:
Joel Coehoorn earned 2000 total points
ID: 20377158
It's typically a good idea to use the existing data structures provided for you.  As it happens, most of the collections already provide a way to see if an item exists.  I'll show you the list, since it's the simplest:

    Dim keys As New System.Collections.Generic.List(Of String)
    keys.Add(strRQEmailID)
   
    If Not keys.Contains(strPLEmailID) Then keys.Add(strPLEmailID)
    If Not keys.Contains(strPMEmailID) Then keys.Add(strPMEmailID)
    If Not keys.Contains(strADEmailID) Then keys.Add(strADEmailID)
    If Not keys.Contains(strTCEmailID) Then keys.Add(strTCEmailID)
    If Not keys.Contains(strTWEmailID) Then keys.Add(strTWEmailID)

    Dim conn As New SqlClient.SqlConnection("your connection string here")
    Dim cmd As New SqlClient.SqlCommand("INSERT INTO PERSON (Email_ID) VALUES (@Email)", conn)
    Dim param As SqlClient.SqlParameter = cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50)
   
    conn.Open()
    For Each key As String In Keys
        param.Value = key
        cmd.ExecuteNonQuery()
    Next key
    conn.Close()

0
 
LVL 18

Expert Comment

by:Joel Coehoorn
ID: 20377175
I should mention that the code I posted assumes you none of the people already exist in the database.
0
 

Author Comment

by:msyed1
ID: 20377419
Thanks for the code, I tried it....but I think Generics are only available in .Net 2.0 and above.  I am using Microsoft .Net Framework 1.1.
How can I do the comparison easily in .Net 1.1 ??
0
 
LVL 18

Expert Comment

by:Joel Coehoorn
ID: 20377540
Try using a system.collections.ArrayList instead.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20556029
Forced accept.
modus_operandi
EE Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question