have a method who gets data from one table, put the data into variables before it it returned into the same table with changed ID etc.
My problem is that it adds the first row without any problems, but when it is supposed to add row number 2 the error says tha the row allready exists in the table. and I can not understand why.
I have put in a couple of messageboxes to se that the ID is changing and that the ordreID is correct and changing as they should and they do, but stll I get that errormessage.

My code looks like this:

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")
        Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow()

        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 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

Annyone who can see why I get that error?

Move the line Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow() so that it's inside the third iteration, just above the line newOrdreRow("OrdreID") = MaxID (this is for fixing the error message).
And add the line Kalkyle1DataSet.AcceptChanges() just after the iteratation.
Also, if OrdreID is the unique identifier you need to increment it on every itereration. Not just once before you start to copy records.
So, change the line newOrdreRow("OrdreID") = MaxID to

MaxID += 1
newOrdreRow("OrdreID") = MaxID
This article has been dead for over six months. Start a new discussion instead.