I have a problem copying rows in a table.


My code is:

Public Sub Ordreupdate()
        Dim conn2 As OleDbConnection
        conn2 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source =|Datadirectory|\Kalkyle1.mdb")


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

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


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

        OrdreTableAdapter.Update(Kalkyle1DataSet.Ordre)


    End Sub

But in the datagrid I use to se the dt the data is ok, but when it pastes the data into the table the data is all wrong.

Why??

I solved this at last.

I read through the code and realized that I was allocating variables from the main table and not from dt. So by replacing al the Kalkyle1DataSet.Tables("Ordre") with dt. it worked just great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.