Solved

Vb.Net - Finding DataRow in DataTable

Posted on 2014-01-06
5
796 Views
Last Modified: 2014-01-22
Good Day Experts!

I am having a bit of trouble working with my DataTable.  Behind my <Get Records> button, I query my SQL Database and fill my DataTable via a DataAdapter.  Then the next time I hit the <Get Records> button I am filling another DataTable.  Then Iterate through the second DataTable and am trying to look for it in the first DataTable.  

Bascially, I am trying to find the records, if any, are in the second DataTable compare to the first.  I am getting an error "Table doesn't have a primary key". I tried adding the 3 Primary Keys from the original SQL table to the DataTable.  Now I am getting an error "These columns currently don't have unique values".

Do I have to make the 3 PrimaryKey lines somehow a composite?

My attempt at code to find row:
For Each dr In tblOverridesTable2.Rows
           MatchRow = tblOverridesTable.Rows.Find(dr)
           If MatchRow Is Nothing Then
                   tblDifferencesTable.Rows.Add(MatchRow)
           End If
Next

My attempt at code to add PrimaryKey:
tblOverridesTable.PrimaryKey = New DataColumn() {tblOverridesTable.Columns("Batch Number")}
tblOverridesTable.PrimaryKey = New DataColumn() {tblOverridesTable.Columns("Entry Order")}
tblOverridesTable.PrimaryKey = New DataColumn() {tblOverridesTable.Columns("Pro Number")}

Is there a better way to find the differences between 2 DataTables with the same structure?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Ess Kay earned 250 total points
ID: 39760264
here you can join the table with another, similar the way SQL join works\ Just have a try on this.



How To: Use DataRelation to perform a join on two DataTables in a DataSet?


Inner join of DataTables in C#
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 250 total points
ID: 39761425
Instead of looping through 2 datatables., query can be modified in sqlserver to retrieve the records with needed scenario.it will be more easy than looping and forming the needed structure in .net code.

OR

Normally the DataTable.Merge method removes duplicates implicitly. But only when all columns' values are the same.

Refer

http://kranthi-kumar-asp-net.blogspot.in/2012/01/merge-two-datatables-by-eliminating.html
http://www.nullskull.com/q/10200050/how-to-merge-the-two-dataset-without--duplicates.aspx
0
 

Author Comment

by:Jimbo99999
ID: 39782782
Good Day Experts!

Finally back to being able to take a look at this little project.  Perhaps my interest is unconventional for what I need to do here.  

I have a SQL table that contains fields that if they contain data I am retrieving those records. Not all records will have data in those fields.  

My plan is to have the User keep my VB Project open on their desktop for the entire day.  At the start of the day, the <GetRecords> button is hit and a DataGrid is loaded with records that have data in the certain fields as described above.  I have not cleared the DataTable that I used to set the DataGrid datasource.  So, a 1/2hr later the User hits the  <GetRecords> button again to get those records that have data in the certain fields.  I have that data in a different DataTable as the previous time the <Get Records> button was hit.

Now, essentially I want to compare those 2 DataTables to see what "New" records are in the second DataTable compare to the first one. Then this can continue throughout the day where the USer would hit the <Get Records> button and I want to compare the previous DataTable to the current DataTable to see the "New" records.

Any thoughts on whether I may be going about this the wrong way? Or perhaps a possible better way to achieve it?

Thanks,
jimbo99999
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39783786
not just use the timestamp?


in the program after get results, set a variable of last accessed



all new records time has a later time than your variable
0
 

Author Closing Comment

by:Jimbo99999
ID: 39799810
Hello, thanks for all your responses.

I am going to split the points among all contributors as I think my idea is not feasible.

Thanks,
jimbo99999
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with error message for ReportViewer in VS2015 4 44
Limiting string to two deciamls 18 50
Create a form which is copy of a form in vb.net 2 34
VS.net 2010 11 23
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now