Save/Update problem

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
I have a method which gets a row from 1 table puts it into a clonetable.
In the clone table I change the values in 2 of the columns, then I use import row to put them back into the original table as a new row.

But when I try to save/update it I get the folowing errormessage:

Error1.jpg




My Code looks like this:

VB.NET:
        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

        MsgBox("Forsøker å Endre data")


        '*********************************************************************
        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
            dt.Rows(n)("OrdreID") = MaxID
            dt.Rows(n)("KundeID") = KundeID
            n = n + 1
        Next
        DataGridView1.DataSource = dt

        MsgBox("Forsøker å legge inn data i ordretabell")

        Dim copyRow1 As DataRow
        Dim copyRow() As DataRow = _
        dt.Select("OrdreID = 3")

        For Each copyRow1 In copyRow
            Kalkyle1DataSet.Tables("Ordre").ImportRow(copyRow1)
        Next

I use a savebutton who calls a statement like this:

VB.NET:
 Dim ordreUpdates() As DataRow = Me.Kalkyle1DataSet.Ordre.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)

and calls with

 Me.OrdreTableAdapter.Update(ordreUpdates)

But I do not understand why I get the errormessage.

I do have a table called prosess_18, who uses OrdreID as a foreign key
But I have not put any data into that table or any of the other tables yet.

But Prosess_18 is the only one I get an error from.
And I haven't changed anything in that table which should cause any problems.

Anyone have a clue where the problem might be?
 
When you import the row back into the Odre table you are adding the a new row into the table. This imported row has an OrderID that is the same as another row in the Odre table. The Process_18 table references tries to reference one instance of the OrderID but there are now 2 instances and so it conflicts with it's constraints.
 
But I do not undrstand why that should happend.
As you see in my code I select a row from table1 and put it into table 2, in table 2 I change the OrdreID for the row from 1 to max. and when I then select the row from table 2 and imports it into table 1 the OrdreID is max and not 1 as it was before. I can see that visually in my Datagridview that the OrdreId is changed.
 
Back
Top