Solved

Vb.Net - Finding DataRow in DataTable

Posted on 2014-01-06
5
790 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

11 Experts available now in Live!

Get 1:1 Help Now