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
Solved

copy datarow vb.net strongly typed Dataset

Posted on 2006-11-30
2
1,702 Views
Last Modified: 2010-05-18
Hi there,

I am using strongly typed datatables. I  have a datatable filled from a dataAdapter. I want to copy some of the rows in the table, add them as new rows to the same the data table (so the same details for all columns) but they should auto generate themselves new keys and then post this back to the database, so under the hood only the newly added rows will get inserted to the DB. I just need an easy way to copy existing rows and do the add as then I should be able to just call Update and RowState.Added rows will get inserted right?

Also if I need to to change one colum's data for the new row's, an Foreign key, would this be easy?

Any help, thanks?
0
Comment
Question by:craigdev
2 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 250 total points
ID: 18049450
you can use the datatables ImportRow method to create a new row based on an existing row.


eg

dim r as datarow = dt.rows(0)
dt.ImportRow(r)



though I don't know how it handles keys etc.
0
 
LVL 16

Assisted Solution

by:RobertRFreeman
RobertRFreeman earned 250 total points
ID: 18072740
ImportRow won't handle keys.

You should use a simple function:

    Private Sub CopyRow(ByVal Table As DataTable, ByVal Index As Integer)
        Dim row As DataRow = Table.NewRow
        For i As Integer = 0 To Table.Columns.Count - 1
            If Not Table.Columns(i).AutoIncrement Then
                row(i) = Table.Rows(Index).Item(i)
            End If
        Next
        Table.Rows.Add(row)
    End Sub


Note:
This will handle autoincrement key columns, but needs to be coded to adjust for unique columns/constraints.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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