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.
SELECT Employee_ID,SSN,Last_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.
private Boolean CheckSSN(String ssn)
{
foreach(DataRow row in yourDataTable.rows)
{
if(ssn == row["SSN"].ToString())
return true;
else
return false;
}
}
{
foreach(DataRow row in yourDataTable.rows)
{
if(ssn == row["SSN"].ToString())
return true;
else
return false;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("SSI D"), MyDataRow.Item("SSID").ToS tring)
Catch ex As Exception
MsgBox(MyDataRow.Item("SSI D").ToStri ng & " 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.
--------------------------
Dim colDuplicate As New Collection
For Each MyDataRow As DataRow In MyDataTable.Rows
Try
colDuplicate.Add(MyDataRow
Catch ex As Exception
MsgBox(MyDataRow.Item("SSI
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.
Bob