Solved

copy datarow vb.net strongly typed Dataset

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now