I try to copy data from rows in my db and then add them with another ID.

but somehow I think I have made a mess out of it.

Public Sub Ordreupdate()

        Dim dtbl As New DataTable
        Dim dt As New DataTable
        dtbl = Kalkyle1DataSet.Tables("Ordre")
        dt = dtbl.Clone

        Dim copyRows1 As DataRow
        Dim copyRows() As DataRow = _
        Kalkyle1DataSet.Ordre.Select("OrdreID = 1")

        For Each copyRows1 In copyRows
        For Each copyRows1 In copyRows
        'DataGridView1.DataSource = dt

        Dim conn = New SqlClient.SqlConnection
        conn = New SqlConnection(Form1.DS2)
        Dim myScalarQuery As String
        Dim ID As Integer
        Dim MaxID As Integer
        Dim KundeID As Integer

        myScalarQuery = " Select Max(OrdreID) As ID From Ordre"
        Dim myCommand As New SqlCommand(myScalarQuery, conn)
        ID = myCommand.ExecuteScalar()
        MaxID = ID + 1
        KundeID = CInt(KundeIDTextBox.Text)

        Dim n As Integer = 0
        For Each copyRows1 In copyRows
            Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow()
            Dim Ordredato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Ordedato")
            Dim Kalkopprettet As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Kalkyle_opprettet")
            Dim LevDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Leveringsdato")
            Dim Virkelig_LevDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Virkelig_leveringsdato")
            Dim Hovedtegningnr As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Hovedtegningnr")
            Dim Prodtype As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Produkttype")
            Dim MatType As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Materialtype")
            Dim KonstrDato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Konstruktor_dato")
            Dim KalkUtarb As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Kalkyle_utarbeidet")
            Dim EtterkalkUtarb As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Etterkalkyle_utarbeidet")
            Dim TegnRev As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Tegningsrevisjon")
            Dim Status As Integer = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Status")

            newOrdreRow("OrdreID") = MaxID
            newOrdreRow("KundeID") = KundeID
            newOrdreRow("Ordedato") = Ordredato
            newOrdreRow("Kalkyle_opprettet") = Kalkopprettet
            newOrdreRow("Leveringsdato") = LevDato
            newOrdreRow("Virkelig_leveringsdato") = Virkelig_LevDato
            newOrdreRow("Hovedtegningnr") = Hovedtegningnr
            newOrdreRow("Produkttype") = Prodtype
            newOrdreRow("Materialtype") = MatType
            newOrdreRow("Konstruktor_dato") = KonstrDato
            newOrdreRow("Kalkyle_utarbeidet") = KalkUtarb
            newOrdreRow("Etterkalkyle_utarbeidet") = EtterkalkUtarb
            newOrdreRow("Tegningsrevisjon") = TegnRev
            newOrdreRow("Status") = Status

            n = n + 1


    End Sub

When I look at it it looks like I copy the data from the table correctly but when I declare and add the data I think I have done something wrong since ex:

Dim Ordredato As String = Kalkyle1DataSet.Tables("Ordre").Rows(n).Item("Ordedato")
points to a spesific row in the table and then ignores the copy I have done before?

I use a datagridview to see what data I have copied and that looks right.

So what I basicly need is to get data from the table for a spesific OrdreID and then add it back in under a new OrdreID.

Can someone give me a hand here?

This may be off base here but this code:

For Each copyRows1 In copyRows

is not going to clear the whole cloned table. It will only clear as many rows as the query OrdreID = 1 produces, not all the rows in the table. It seems you would want to start with an empty table, copy the records you want into it, then run and update query on each record to change the ID to the new ID.

This article has been dead for over six months. Start a new discussion instead.