Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

I didn't 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?

  • *Experts*
Posted (edited)

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 there's a match assign some value true.

      
       Dim dr As DataRow

       Dim key As Integer = 0    'in your database, the column that holds 
                         'the key value who's 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" who's 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 you're certain you want to delete that row, then add this

after the if statement in the above 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.

Edited by jfackler
Posted

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).

  • *Experts*
Posted

The datarows aren't an array either, they're 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).

Posted

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?

  • *Experts*
Posted

Replace the "some value" with the value of your name code.

If that's 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.

  • *Experts*
Posted

There's an easier method to determine dupes in a DataSet. It might not be as speedy as other methods, but it's easy to understand AND code.

 

First, create a DataRow array using the DataTable's Select method with a sort on the Primary Key or Keys (depending on whether your table's "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 they're duplicates.

 

Here's 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:

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())
       // There's a dupe at index i

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...