user2150279
user2150279

Reputation: 475

How to check if datarow exists in datable without using primary key method

I am using VB .NET and I want to check if a Datarow (Row A) from a Datatable(Table A) exists in another datatable (Table B).

I had tried using the code below:

TableA.Rows.Contains(RowA)

I got an error message stating: "MissingPrimaryKeyException was unhandled by user code. Table does not have a primary key".

Could you suggest how can I search a datarow in a datatable without using primary key method?

Thank you.

Upvotes: 1

Views: 3947

Answers (2)

Derek
Derek

Reputation: 8773

Dim i As Integer = (datatable.Rows.Count - 1)
Do While (i >= 0)
    If datatable.Rows(i)("Column1") = RowA("Column1") And datatable.Rows(i)("Column2") = RowA("Column2")  Then
        datatable.Rows(i).Delete
    End If
    i = i - 1
Loop

You will have to change the names of the columns, of course.

Upvotes: 3

Victor Zakharov
Victor Zakharov

Reputation: 26454

Check row state of RowA, if it's Detached, the table does not contain it.

Without primary key, I don't know how you can make it work any better than that.

Upvotes: 0

Related Questions