Solved

copy datarow vb.net strongly typed Dataset

Posted on 2006-11-30
2
1,695 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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