Solved

Vb.Net - Finding DataRow in DataTable

Posted on 2014-01-06
5
832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access/Visual Basic Question 3 43
Access Schema 6 45
vb.net sendkey 4 19
Copying from a network share 3 26
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…

726 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