?
Solved

Copy datatable into new datatable and rename columns

Posted on 2011-09-21
3
Medium Priority
?
443 Views
Last Modified: 2012-05-12
I have a datatable with the following columns

[start Date]
[Pmt Amount]
[Compounding]

How do I copy 2 of those columns into a new datatable to pass to SQL 2008 as a structured data table

the new datatable would be
startDate
PaymentAmt
0
Comment
Question by:lrbrister
[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
3 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 2000 total points
ID: 36575647
I think you don' t need create other table.
You can Map columns from your table to Parameters on Command that update Sql 2008.

Note that it is very easy copy to other table, but I need know:

   Copy only current values ?
   Copy Deleted Rows ?

Note that if you use DataAdapter Update you need copy rowstate and also original an current values.

To do this

   dim ccOr = new dataColumn(){ OrignalTabl.Column("start Date"), OrignalTabl.Column("Pmt Amount"),
   Dim ccDes = new dataColumn(){ OrignalTabl.Column("startDate"), OrignalTabl.Column("PaymentAmt "),

   For each Row as Datarow in OriginaTable
      dirm rDes = DestTable.newRow
      for n=0 to ccor.length -1
         rdest(ccdes(n)) = row(ccor, DataRowVersion.Original)
      next
      rdes.rows.add(rdest)
   next

   Tdest.acceptchanges

Now, write code to delete deleted rows and Update modified rows

   for i as integer = 0 to OriginaTable.rows.count -1
       select case OriginaTable.rows(i).rowstate
            Case DataRowState.Modified
                   dim row=  OriginaTable.rows(i), rdest=DestTable.rows(i)
                   for n=0 to ccor.length -1
                        rdest(ccdes(n)) = row(ccor)
                   next
            Case DataRowState.Deleted
                destTable.rows(N).delete
       end select
   next
0
 
LVL 15

Expert Comment

by:x77
ID: 36575663
Note. I write code directly, I don´t verify.

by sample :  where says         row(ccor)    ->    row(ccor(n))

0
 

Author Closing Comment

by:lrbrister
ID: 36576045
You put me on the right track...thanks

Dim dt As New DataTable
dt = ConvertTo(l_lead.Payments)
dt.Columns(0).ColumnName = "LeadType"
blah...blah...blah..

dt.Columns.Remove("LeadType")
dt.Columns.Remove("PaymentStreamID")
blah...blah...blah..

dt.Columns("EventType").SetOrdinal(0)
dt.Columns("StartDate").SetOrdinal(1)
dt.Columns("PmtAmount").SetOrdinal(2)
blah...blah...blah.."
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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