Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copy datarow vb.net strongly typed Dataset

Posted on 2006-11-30
2
Medium Priority
?
1,779 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 750 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 750 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

783 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