Solved

Vb.Net - Finding DataRow in DataTable

Posted on 2014-01-06
5
818 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No Data for DropDown List 2 28
vb.net 1 month apart 11 35
VB.NET 2008 - SQL Timeout 9 34
How do you pull mopre then 1000 records using VB.NET Directory Services namespace 2 33
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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