Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vb.Net - Finding DataRow in DataTable

Posted on 2014-01-06
5
Medium Priority
?
938 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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 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