Record Existence Check on DataTable

Posted on 2007-08-10
Last Modified: 2013-11-07
I have a Datatable filled with this query:

SELECT Employee_ID,SSN,Last_Name,First_Name
FROM Employee_Table

The primary key is Employee_ID.  

Basically, I want to find if a particular row exists on this DataTable by looking at the SSN, instead of the Employee_ID.  The SSN is a unique key, but not the primary key.

I just want a boolean flag that returns if a matching SSN record exists.

How do I do this?

I am using Visual Basic 2005, Visual Studio 2005.

Thank you.
Question by:crickpaolo
    LVL 96

    Expert Comment

    by:Bob Learned
    You could use a Select Count(*) From Employee_Table Where SSN = @SSN query, an use an ExecuteScalar call from a command object.

    LVL 22

    Expert Comment

    private Boolean CheckSSN(String ssn)
          foreach(DataRow row in yourDataTable.rows)
                if(ssn == row["SSN"].ToString())
                  return true;
                  return false;      

    LVL 96

    Accepted Solution

    This is a VB.NET question:
       Dim rows() As DataRow = dt.Select(String.Format("SSN = '{0}' ", ssn))

    LVL 14

    Expert Comment

    Since you have all the data locally, you may want to try:
          Dim colDuplicate As New Collection
          For Each MyDataRow As DataRow In MyDataTable.Rows
                colDuplicate.Add(MyDataRow.Item("SSID"), MyDataRow.Item("SSID").ToString)
             Catch ex As Exception
                MsgBox(MyDataRow.Item("SSID").ToString & " Is A Duplicate.")
             End Try
          colDuplicate = nothing
    Note: Off the top of my head I don't recall the Duplicate collection error number. Hopefully, you get the idea.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now