im using this code to try inserting 3 rows at the same time:
Private Sub btnBRR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBRR.Click
If btnBRR.Text = "Borrow" Then
con.Open()
Try
Dim rowa, rowb, rowc As String
For i As Integer = 0 To Me.dgvIssuedBooks.Rows.Count
rowa = Me.dgvIssuedBooks.Rows(i).Cells(0).ToString()
rowb = Me.dgvIssuedBooks.Rows(i).Cells(1).ToString()
rowc = Me.dgvIssuedBooks.Rows(i).Cells(2).ToString()
da = New OleDbDataAdapter("insert into IssueBooks values('" & rowa & "','" & rowb & "','" & rowc & "')", con)
Next
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
End Try
con.Close()
End If
End Sub
but i've got this kind of error message
Quote
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
i also tried this other code,
Private Sub btnBRR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBRR.Click
If btnBRR.Text = "Borrow" Then
con.Open()
Try
Dim i As Integer
Dim dtmTest As Date
dtmTest = DateValue(Now)
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt) 'DataSet to DataTable
da = New OleDbDataAdapter("Insert into IssueBooks(AccessionNumber, CallNumber, Title, ID, Borrower, DateBorrowed, DueDate) values ('" & dgvIssuedBooks.Rows(i).Cells("AccessionNumber").Value & "','" & dgvIssuedBooks.Rows(i).Cells("CallNumber").Value & "','" & dgvIssuedBooks.Rows(i).Cells("Title").Value & "','" & dgvSearch.Rows(i).Cells("ID").Value & "', '" & dgvSearch.Rows(i).Cells("FullName").Value & "','" & dtpBorrow.Text & "', '" & dtmTest & "')", con)
da.Fill(dt)
MsgBox("Book Borrowed")
txtAccess2.Text = ""
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
End Try
con.Close()
End If
End Sub
it WORKS but it inserts only one row into my db even my dgv has 2 or more rows.
can someone help me in inserting more than one row in the database using dgv.

New Topic/Question
Reply



MultiQuote







|