Link to home
Start Free TrialLog in
Avatar of crickpaolo
crickpaolo

asked on

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Bob
Avatar of p_davis
p_davis

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

      }
}
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.