?
Solved

Create new Datatable from two linked datatables

Posted on 2013-05-17
7
Medium Priority
?
290 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

741 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