multiple rows in add row

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
I 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:
VB.NET:
 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
            dt.Rows.Clear()
        Next
        For Each copyRows1 In copyRows
            dt.ImportRow(copyRows1)
        Next
        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

        conn.Open()
        myScalarQuery = " Select Max(OrdreID) As ID From Ordre"
        Dim myCommand As New SqlCommand(myScalarQuery, conn)
        ID = myCommand.ExecuteScalar()
        conn.Close()
        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


            Kalkyle1DataSet.Tables("Ordre").Rows.Add(newOrdreRow)
            n = n + 1
        Next

        SaveProsess()

    End Sub
Annyone who can see why I get that error?
 
I solved it by putting:
Dim newOrdreRow As DataRow = Kalkyle1DataSet.Tables("Ordre").NewRow()
into the iteration and Kalkyle1DataSet.AcceptChanges() right after.
 
Back
Top