sing1006 -5 Light Poster

I had 3 datagridview in visual basic, and each of them is linked to each other.
1 Main component can have many Failure Mode and 1 Failure Mode can have many Critical.
Click Here
Click Here
Click Here

I want to duplicate selected row or record including their children details, I manage to duplicate all the details for first main component but unable to duplicate the second. Below are by coding

cmd.CommandText = "INSERT INTO tbl_MainComp (EquipTag, MC_Name, MC_Function) " &
                " SELECT '" & TextBox1.Text & "', MC_Name, MC_Function FROM tbl_MainComp WHERE EquipTag ='" & Tbl_SystemDataGridView.CurrentRow.Cells(0).Value & "'"
                cmd.ExecuteNonQuery()

                Dim DMC_No As New List(Of Integer)
                Dim DFM_No As New List(Of Integer)
                cmd.CommandText = "SELECT MC_No FROM tbl_MainComp WHERE EquipTag=  '" & TextBox1.Text & "'"
                Using reader As OleDb.OleDbDataReader = cmd.ExecuteReader()
                    While reader.Read()
                        DMC_No.Add(reader.GetInt32(reader.GetOrdinal("MC_No")))
                    End While
                End Using

                 For i = 0 To DMC_No.Count - 1
                    cmd.CommandText = "INSERT INTO tbl_SubComp (MC_No, SC_Name) " &
                                       " SELECT " & DMC_No.Item(i) & ", SC_Name FROM tbl_SubComp WHERE MC_No =" & Tbl_MainCompDataGridView.Rows(i).Cells(0).Value & ""
                    cmd.ExecuteNonQuery()
                    cmd.CommandText = "INSERT INTO tbl_FailureMode(MC_No, FM_Description,FM_FailureEffects,FM_LocalEffects,FM_EventualEffects,FM_ENdEffect, C_YN, RTF_YN, RTF_SimpleTask) " &
                                    " SELECT " & DMC_No.Item(i) & ", FM_Description,FM_FailureEffects,FM_LocalEffects,FM_EventualEffects,FM_ENdEffect, C_YN, RTF_YN, RTF_SimpleTask FROM tbl_FailureMode WHERE MC_No =" & Tbl_MainCompDataGridView.Rows(i).Cells(0).Value & ""
                    cmd.ExecuteNonQuery()

                    cmd.CommandText = "SELECT FM_No FROM tbl_FailureMode WHERE MC_No=  " & DMC_No(i)
                    Using reader2 As OleDb.OleDbDataReader = cmd.ExecuteReader()
                        While reader2.Read()
                            DFM_No.Add(reader2.GetInt32(reader2.GetOrdinal("FM_No")))
                        End While
                    End Using

     For c = 0 To DFM_No.Count - 1
                    cmd.CommandText = "INSERT INTO tbl_Critical(FM_No, C_Cause,C_Task,C_Status,C_Action) " &
                                                 " SELECT " & DFM_No.Item(c) & ", C_Cause,C_Task,C_Status,C_Action FROM tbl_Critical WHERE FM_No =" & Tbl_FailureModeDataGridView.Rows(c).Cells(0).Value & ""


                    cmd.ExecuteNonQuery()

                Next c
                Next i

I think something wrong with my for loop

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.