• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

Add Multiple Rows to a DataSet / DataTable

Hellow everyone.  I am running into a slight problem and cant seem to figure it out.
I am pulling Data from one table:
<<Code>>
        sqldaSelectCond.SelectCommand.Parameters("@prgm").Value = varLoanProgram
        DsSelectConditions1.tbl_list_conditions.Clear()
        sqldaSelectCond.Fill(DsSelectConditions1.tbl_list_conditions)
<<End Code>>
This will pull over about 10 to 15 records and fill the DsSelectConditions DataSet.
I then need to add those 10 or so records to the other Dataset(DsConditions)

How can I do this.
Here is my code for adding 1 record.
<<Code>>
      drConditions = dsTeamplateDB1.tblConditions.NewRow()
      drConditions.Item("PID") = varPID
      drConditions.Item("LoanID") = varLoanID
      drConditions.Item("condition") = varCondition
      dsTeamplateDB1.tblConditions.Rows.Add(drConditions)
<<End Code>>

Your help will be greatly appreciated.
0
danorme
Asked:
danorme
  • 4
  • 3
1 Solution
 
SanclerCommented:
I'm not sure I've got the full picture.  You say you want to add records from one dataset - DsSelectConditions - to the other dataset - DsConditions.  But the code you show for adding new records refers to a third dataset - dsTeamplateDB1.  So I'll refer to SourceDataTable and DestinationDataTable and leave you to put the proper references in.

I'll also assume that the SourceDataTable and the DestinationDataTable have identical structures - number of columns and datatypes.

On that basis the code should be

   For Each dr as DataRow In SourceDataTable.Rows
      Dim newdr As DataRow = DestinationDataTable.NewRow
      newdr.ItemArray = dr.ItemArray
      DestinationDataTable.Rows.Add(newdr)
   Next

Roger
0
 
danormeAuthor Commented:
My bad.  I need to also include two variables for each of those entries. varPID and varLoanID.
Kind of like this.
      drConditions.Item("PID") = varPID
      drConditions.Item("LoanID") = varLoanID
Where do I put this in the code that was so graciously provided.

0
 
SanclerCommented:
As I said "I'm not sure I've got the full picture" ;-)

Are we talking about - in the terms I used - the SourceDataTable or the DestinationDataTable?  That is, do you want to put those variables in the record before you copy it from one table to the other, so that then both the original and the copied record will have those values in them?  Or do you want to put those variables in the record after you copy it from one table to the other, so that then only one of the versions - the original or the copied record - will have them in it?  And if it is the latter, do you wish to put them in the original or in the copy?

Roger

PS I'm just off out for the evening, but will come back to you ASAP after your response when I return.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
danormeAuthor Commented:
I need to add those variables to the copied records only.  

I really do appreciate your help.
0
 
danormeAuthor Commented:
Ya know.  There is one more part to this.  The two tables are not exactly alike.

So, I guess this is what I need to do.
Take the original data
Map each of its fields to the fields of the new (destination) table.
0
 
SanclerCommented:
>>
So, I guess this is what I need to do.
Take the original data
Map each of its fields to the fields of the new (destination) table.
<<

Got it in one!

What's the structure of the two tables?  Are the common columns in the same order and contiguous?  If so you can simply cycle through the common columns and then fill the two others at the end, like this

   For Each dr as DataRow In SourceDataTable.Rows
      Dim newdr As DataRow = DestinationDataTable.NewRow
      For i As Integer = FirstCommonColumnNumber To LastCommonColumnNumber
         newdr.Item(i) = dr.Item(i)
      Next i
      newdr.Item("PID") = varPID
      newdr.Item("LoanID") = varLoanID
      DestinationDataTable.Rows.Add(newdr)
   Next

Or - eg if the "new" columns in the destination go at the beginning - it might be necessary to add an offset to the "copying" line, like this

         newdr.Item(i + 2) = dr.Item(i)

If the common columns aren't in the same order and contiguous, you will need to map each column specifically in code, on these lines

   For Each dr as DataRow In SourceDataTable.Rows
      Dim newdr As DataRow = DestinationDataTable.NewRow
      newdr.Item(0) = dr.Item(1) 'using index number ... and/or
      newdr.Item("ThisColName") = dr.Item("ThatColName") 'using column names
      'etc, then
      newdr.Item("PID") = varPID
      newdr.Item("LoanID") = varLoanID
      DestinationDataTable.Rows.Add(newdr)
   Next

I almost hesitate to mention this.  But are the varPID and varLoanID the same for all "copied" records?

Roger
0
 
danormeAuthor Commented:
varPID and varLoanID are the same for the copied records.

Got it.  Awsome!  I started VB 8 months ago and with the help of all you guys, my boss, and several books I have been able to make one heck of a program for my company.  Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now