Results 1 to 4 of 4

Thread: copy datarow in other datatable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Belgium
    Posts
    98

    copy datarow in other datatable

    Hi,

    I am trying to create a procedure that can copy datarows in other datatables. I have a dataset with one datatable and this datatable contains 10 datarows. I want to create 10 seperate datasets each containg one datatable. This datatable contains only one datarow. This datarow should be a copy of the datarow in the first datatable (the one with the 10 datarows).

    Thanks for reading my problem.

    This is my test code, but it does not work.

    Code:
            'connection class 
            Dim c As New clsSQLConnection("localhost", "northwind", "sa", "sa")
            'dataset class 
            Dim d As New clsSQLDataset(c)
            'fill the dataset 
            d.fillDataset(Chr(34) & "sales by year" & Chr(34) & " '1990', '2004'")
            'a new collection 
            Dim coll As New Collection
    
            Dim tmpDS As DataSet
            Dim tmpTable As DataTable
            Dim tmpRow As DataRow
    
    
            For Each dr As DataRow In d.dtSet.Tables(0).Rows
    
                tmpDS = New DataSet
                tmpTable = New DataTable
                tmpTable.Rows.Add(dr)
                'An unhandled exception of type 'System.ArgumentException' 
                'occurred in system.data.dll
                'Additional information: 
                'This row already belongs to another table. 
                tmpDS.Tables.Add(tmpTable)
                coll.Add(tmpDS)
            Next
    
            MsgBox(coll.Count)
    Last edited by R@emdonck; Mar 15th, 2004 at 06:40 PM.

  2. #2
    Fanatic Member alexandros's Avatar
    Join Date
    Oct 2002
    Location
    Milky Way Galaxy
    Posts
    694

    Re: copy datarow in other datatable

    i got exactly the same problem.
    any help ?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: copy datarow in other datatable

    You don't copy a DataRow TO another DataTable, but rather you copy it FROM another DataTable. Check out the DataTable.ImportRow method.

    For the original problem, you would call Clone on the original DataTable to replicate the structure and then ImportRow on the new DataTable:
    VB Code:
    1. Dim newDataSet As New DataSet
    2. Dim newDataTable As DataTable
    3.  
    4. For i As Integer = 0 To myTable.Rows.Count - 1 Step 1
    5.     newDataTable = myTable.Clone()
    6.     newDataTable.ImportRow(myTable.Rows(i))
    7.     newDataSet.Tables.Add(newDataTable)
    8. Next i
    Last edited by jmcilhinney; Nov 2nd, 2005 at 07:09 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member alexandros's Avatar
    Join Date
    Oct 2002
    Location
    Milky Way Galaxy
    Posts
    694

    Re: copy datarow in other datatable

    thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width