about your "somevalue'

arun_mrk

Well-known member
Joined
Apr 1, 2003
Messages
51
Location
beijing
duplicate rows in dataset?

I have a dataset which will have duplicate rows and some time it might not have duplicate rows.

I would like to write a function to check for duplicate row in dataset and return true if duplicate rows are found else i want to return false

How can i do it?
 
I didnt build the dataset from the database.
I built it based on the user inputs. It big Process to explain how the duplicate rows where added.
But all that i can say is i have a dataset with many rows.
I want to write a function to check if there are any duplicate rows if yes, the should return "false" else it should return "true".
My only problem is how to check whether a duplicate row exists or not?
 
Is there a field that, if you checked it, the entire row would definitely be duplicated?
If so, the rows of data are a collection and as such you can do a for each and check for a match to some value,
then if theres a match assign some value true.
Code:
        Dim dr As DataRow

        Dim key As Integer = 0    in your database, the column that holds 
                          the key value whos row your looking to match

        Dim int As Integer =0

         Dim bc As Integer = 0

        For Each dr In dataset1.Tables("MyTable").Rows   array of datarows

         If CStr(dr.Item(key)) Is "SomeValue" Then  interogates each datarow for the 
                                            key value "SomeValue" whos row you want to edit


         bc = int    sets binding context to row that holds key value


         End If

         int  += 1   counter for rows...next row

        Next

You may want to create an array to hold the matches or,
if youre certain you want to delete that row, then add this
after the if statement in the above code:

Code:
         bc = int    sets binding context to row that holds key value


         End If

        dataset1.Tables("MyTable").Rows(bc).Delete()

         int  += 1   counter for rows...next row

        Next

And it will delete all those that match in the dataset.
 
Last edited by a moderator:
i have some doubts in the above code.

You have written " i.item" in the if part.
What is i?
and what should i put(subsitute) in "some value".

In my dataset i have a key column(name code) if its same than it means the row is similiar(duplicate).
 
The datarows arent an array either, theyre a collection.

cstr(dr.item(key)) should return the value as a string of
the value held in the first column (0) of your dataset.
You can change that to any column as an integer that holds your
Key (in your case, the name code).
 
i understand clearly about the dr.Item(key).

In your code u had used somevalue, what do u expect me to substute in that place.

dr.Item(key) will have all my code in the dataset, u want me to compare these code with what value?

actually i want to find the duplicate codes?
 
Replace the "some value" with the value of your name code.
If thats an integer, change your cstr to cint....etc.
You can create a collection (beyond this discussion) and add the datarows that are matches at the same place I
added the delete statement in the second code snippet.
That will give you a collection of rows that are duplicate rows.
 
Answered in your original post.

Look there.

[edit]I merged the two threads[/edit]
 
Last edited by a moderator:
Theres an easier method to determine dupes in a DataSet. It might not be as speedy as other methods, but its easy to understand AND code.

First, create a DataRow array using the DataTables Select method with a sort on the Primary Key or Keys (depending on whether your tables "duplicates" are based on one column or more than one). Loop through each row starting at the second row and compare the same column(s) to the previous row. If they match, then theyre duplicates.

Heres some sample C# code (untested). I assume the PrimaryKeyCol is a string (hence the ToString method below). If your primary key column is an ID, cast the column as an int or whatever you need:
C#:
DataRow[] dupes = ds.Tables["DupeTable"].Select(String.Empty, "PrimaryKeyCol");
for(int i=1; i<dupes.Length; i++)
    if(dupes[i]["PrimaryKeyCol"].ToString() == dupes[i-1]["PrimaryKeyCol"].ToString())
        // Theres a dupe at index i

-Nerseus
 
Back
Top