• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1185
  • Last Modified:

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.
  • 2
1 Solution
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;      

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

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now