Solved

copy datarow vb.net strongly typed Dataset

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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