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

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

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

      }
}
0
 
wsh2Commented:
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
         Try
            colDuplicate.Add(MyDataRow.Item("SSID"), MyDataRow.Item("SSID").ToString)
         Catch ex As Exception
            MsgBox(MyDataRow.Item("SSID").ToString & " Is A Duplicate.")
         End Try
      Next
      colDuplicate = nothing
-------------------------------------------------------------------------------
Note: Off the top of my head I don't recall the Duplicate collection error number. Hopefully, you get the idea.
0
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.