Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create new Datatable from two linked datatables

Posted on 2013-05-17
7
Medium Priority
?
296 Views
Last Modified: 2013-06-06
Hi
I have two Datatables, dtRenewals and dtAgents. There is a field in each called AgentCode.
I'd like to create another table, dtRenewals_WithAgents, which would only contain the renewals that have an AgentCode that is in dtAgents.
How would I do this?
Thanks
0
Comment
Question by:jdhackett
[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
  • 3
  • 3
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39175319
You can loop through dtAgents, find associated rows from dtrenewals and add to a new datatable.
0
 
LVL 1

Author Comment

by:jdhackett
ID: 39180564
@srosebabu - those links are for merging two different tables. I don't want to merge the tables, I just want the rows from dtRenewals where the Agent is in dtAgents.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Rose Babu
ID: 39184081
can you provide some sample data for the both data-tables with the required merged data sample?
0
 
LVL 1

Author Comment

by:jdhackett
ID: 39199123
Sample for dtRenewals:
PolicyRef = TEST09
AgentCode = ARC1
IssueDate = 31/04/13

PolicyRef = TEST10
AgentCode = ""
IssueDate = 31/04/13

PolicyRef = TEST11
AgentCode = BAR
IssueDate = 31/04/13


Sample for dtAgents:
AgentCode = ARC1
AgentName = Arch Insurances

AgentCode = ZAP
AgentName = Zap Insurances


In this example, I would only like the row for TEST09.
0
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 39204384
Have a look and test the below code....

Dim drTest As DataRow

Dim dtRenewals As New DataTable
dtRenewals.Columns.Add("PolicyRef")
dtRenewals.Columns.Add("AgentCode")
dtRenewals.Columns.Add("IssueDate")

drTest = dtRenewals.NewRow
drTest(0) = "TEST09"
drTest(1) = "ARC1"
drTest(2) = "31 / 4 / 13"
dtRenewals.Rows.Add(drTest)

drTest = dtRenewals.NewRow
drTest(0) = "TEST10"
drTest(1) = ""
drTest(2) = "31 / 4 / 13"
dtRenewals.Rows.Add(drTest)

drTest = dtRenewals.NewRow
drTest(0) = "TEST11"
drTest(1) = "BAR"
drTest(2) = "31 / 4 / 13"
dtRenewals.Rows.Add(drTest)

Dim dtAgents As New DataTable
dtAgents.Columns.Add("AgentCode")
dtAgents.Columns.Add("AgentName")

drTest = dtAgents.NewRow
drTest(0) = "ARC1"
drTest(1) = "Arch Insurances"
dtAgents.Rows.Add(drTest)

drTest = dtAgents.NewRow
drTest(0) = "ZAP"
drTest(1) = "Zap Insurances"
dtAgents.Rows.Add(drTest)


Dim dtRenewals_WithAgents As New DataTable
dtRenewals_WithAgents.Columns.Add("PolicyRef")
dtRenewals_WithAgents.Columns.Add("AgentCode")
dtRenewals_WithAgents.Columns.Add("IssueDate")
dtRenewals_WithAgents.Columns.Add("AgentName")

Dim strPolicyRef As String = String.Empty
Dim strAgentCode As String = String.Empty
Dim strIssueDate As String = String.Empty
Dim strAgentName As String = String.Empty



If dtRenewals.Rows.Count > 0 Then
	For i = 0 To dtRenewals.Rows.Count - 1
		strPolicyRef = dtRenewals.Rows(i).Item("PolicyRef").ToString.Trim
		strAgentCode = dtRenewals.Rows(i).Item("AgentCode").ToString.Trim
		strIssueDate = dtRenewals.Rows(i).Item("IssueDate").ToString.Trim

		For j = 0 To dtAgents.Rows.Count - 1
			If strAgentCode = dtAgents.Rows(j).Item("AgentCode").ToString.Trim Then
				strAgentName = dtAgents.Rows(j).Item("AgentName").ToString.Trim

				drTest = dtRenewals_WithAgents.NewRow
				drTest(0) = strPolicyRef
				drTest(1) = strAgentCode
				drTest(2) = strIssueDate
				drTest(3) = strAgentName

				dtRenewals_WithAgents.Rows.Add(drTest)
			End If
		Next
	Next
	
	' check the dtRenewals_WithAgents data table for the required record(s)

End If

Open in new window

This will get you the records as you wanted.... Let me know the outcome
0
 
LVL 1

Author Comment

by:jdhackett
ID: 39224873
Very neat, thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…

609 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