0

This code is recording the inserted IDs in arraylist and when press on button it should create loop for this array and get the data of each ID from the array and put it in Datatable and add the new data while looping to the datatable and finaly show it in datagridview . The problem in the result when I insert one record it works fine but when I insert more than one the datagridview shows just the last one , what the mistake that I Done ?!!

In Mainform

Public Inserted_record_hold_dt As New DataTable
Public Inserted_record_dt As New DataTable
Public Sub Addcolumnstodatagrid()
    Inserted_record_dt.Columns.Add("ID")
    Inserted_record_dt.Columns(0).AutoIncrement = True

    Inserted_record_dt.Columns.Add("drawingname")
    Inserted_record_dt.Columns.Add("serial")

End Sub

and call this in main_Load

Addcolumnstodatagrid()

And this in the show button when click to loop on the array list that already have the latest ID's that has been added

Private Sub show_btn_Click(sender As System.Object, e As System.EventArgs) Handles show_btn.Click
    Dim InsertedID As Integer
    Inserted_record_dt.Clear()
    Dim R As DataRow = Inserted_record_dt.NewRow

    'Loop For each ID in the array "Inserted_List_Array"
    For Each InsertedID In mainadd.Inserted_List_Array
        'MsgBox(InsertedID.ToString)
        Dim cmd As New SqlCommand("select drawingname , serial from main where drawingid = '" & InsertedID & "'", DBConnection)
        DBConnection.Open()
        Inserted_record_hold_dt.Load(cmd.ExecuteReader)
        Try

            R("drawingname") = Inserted_record_hold_dt.Rows(0).Item(0)
            R("serial") = Inserted_record_hold_dt.Rows(0).Item(1)
            Inserted_record_dt.Rows.Add(R)

        Catch
        End Try
        'MsgBox("added")
        DBConnection.Close()
        cmd = Nothing
        Inserted_record_hold_dt.Clear()
    Next
    sendmail.Show()
    sendmail.Mail_DGView.DataSource = Inserted_record_dt
End Sub

Please tell me what is the problem in my code .

1 Answer 1

1

Your mistake is in declaring the R variable just one time outside the loop. In this way you continuously replace the values on the same instance of a DataRow and insert always the same instance.

Just move the declaration inside the loop

For Each InsertedID In mainadd.Inserted_List_Array
    ......
    Try
        Dim R As DataRow = Inserted_record_dt.NewRow
        R("drawingname") = Inserted_record_hold_dt.Rows(0).Item(0)
        R("serial") = Inserted_record_hold_dt.Rows(0).Item(1)
        Inserted_record_dt.Rows.Add(R)

    Catch
    ....
Next

Another important thing to do is to remove the empty Try/Catch because you are just killing the exception (no message, no log) and thus you will never know if there are errors in this import. At the end you will ship a product that could give incorrect results to your end user.

Sign up to request clarification or add additional context in comments.

2 Comments

I made what you told me to do and it works but the count of the ID in datagridview is not working correct , When i Insert one record the ID started with 0 and when I insert another record the ID start with 1 for the first record and the second be 2 , If I insert another record the ID start with 2 and the record becomes 2 and the third becomes 4
You can choose where to start the counter for ID with Inserted_record_dt.Columns(0).AutoIncrementSeed = 1 when you define the datatable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.