Solved

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

Posted on 2007-11-29
6
148 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
6 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
Comment Utility
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:jcoehoorn
Comment Utility
I should mention that the code I posted assumes you none of the people already exist in the database.
0
 

Author Comment

by:msyed1
Comment Utility
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:jcoehoorn
Comment Utility
Try using a system.collections.ArrayList instead.
0
 
LVL 1

Expert Comment

by:modus_operandi
Comment Utility
Forced accept.
modus_operandi
EE Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now