Click here to Skip to main content
15,902,800 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Greetings, I would like to know what's wrong with my code. What i want to do is add the inputted datagridview records in the database when I clicked the ADD button. But I'm getting an error (included in the remark), pls help me I need to finish my thesis on Friday, thank you very much.

VB
da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        Dim newRow2 As DataRow
        For i = 0 To poDG.Rows.Count - 1
            newRow2 = ds2.Tables(1).NewRow
            col1 = poDG.Rows.Item(i).Cells(0).Value
            col2 = poDG.Rows.Item(i).Cells(1).Value
            col3 = poDG.Rows.Item(i).Cells(2).Value
            col4 = poDG.Rows.Item(i).Cells(3).Value
            col5 = poDG.Rows.Item(i).Cells(4).Value
            newRow2(0) = col1
            newRow2(1) = col2
            newRow2(2) = col3
            newRow2(3) = col4
            newRow2(4) = col5
            ds2.Tables(1).Rows.Add(newRow2) ' This row already belongs to another table.
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next
Posted
Comments
Pradeep Shukla 12-Sep-11 23:19pm    
da2.Fill(ds2, "PurchaseDB2") would create the first datatable in ds2 dataset, how the other datatable is created in ds2 ? The code should work depending if both the datatables in the dataset are independent of each other.
Joanne0005 12-Sep-11 23:24pm    
i just saw that code in a tutorial, and that code works for him properly.
Joanne0005 12-Sep-11 23:25pm    
so you're saying, i should remove ds2.Tables(1).Rows.Add(newRow2)?
Pradeep Shukla 12-Sep-11 23:31pm    
No, you don't need to remove it, you need to define it in a way so that its not related to the first datatable even though they have the same schema..you can fill both the tables like this:

da2.Fill(ds2, "PurchaseDB2")
da2.Fill(ds2, "PurchaseDB3")
ds.Tables(1).clear ' This would clear the contents
Joanne0005 13-Sep-11 0:19am    
sorry i don't get it, what i want to do is add the inputted record from datagridview to my database, not clear them.

1 solution

Thank you very much prdshukla, i get it now and it's now working properly. Here's the changes that i made:

VB
da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        ds2.Tables("PurchaseDB2").Clear()   ' i added this
        Dim newRow2 As DataRow
        For i = 0 To poDG.Rows.Count - 1
            newRow2 = ds2.Tables("PurchaseDB2").NewRow
            col1 = poDG.Rows.Item(i).Cells(0).Value
            col2 = poDG.Rows.Item(i).Cells(1).Value
            col3 = poDG.Rows.Item(i).Cells(2).Value
            col4 = poDG.Rows.Item(i).Cells(3).Value
            col5 = poDG.Rows.Item(i).Cells(4).Value
            newRow2(0) = col1
            newRow2(1) = col2
            newRow2(2) = col3
            newRow2(3) = col4
            newRow2(4) = col5
            ds2.Tables("PurchaseDB2").Rows.Add(newRow2)
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900