Record Existence Check on DataTable

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.
Who is Participating?
Bob LearnedCommented:
This is a VB.NET question:
   Dim rows() As DataRow = dt.Select(String.Format("SSN = '{0}' ", ssn))

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

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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.