0

it can only save 1 record, but i want to save 2 or more from listview to database
database table: table1 , fields [receiptnum,nod,qty,price,total [all in text]]

For Each x As ListViewItem In lvorder.Items
            sql = "insert into table1 (receiptnum,nod,qty,price,total) values ('" _
               & lblos.Text & "','" _
               & x.SubItems(0).Text & "','" _
               & x.SubItems(1).Text & "','" _
               & x.SubItems(2).Text & "','" _
               & lbltotal.Text & "')"
        Next

        Dim cmd = New OleDb.OleDbCommand(sql, con)
        cmd.ExecuteNonQuery()
        MsgBox("save")
        Me.Refresh()
2
Contributors
2
Replies
11
Views
2 Years
Discussion Span
Last Post by jez9
1

The codes are quite right.
Why do you write the codelines 10 & 11 after complition of the loop. It could save the last row of your listview control. To save every rows from listview control write them into the loop.
The codes should be

For Each x As ListViewItem In lvorder.Items
    sql = "insert into table1 (receiptnum,nod,qty,price,total) values ('" _
          & lblos.Text & "','" _
          & x.SubItems(0).Text & "','" _
          & x.SubItems(1).Text & "','" _
          & x.SubItems(2).Text & "','" _
          & lbltotal.Text & "')"

    Dim cmd = New OleDb.OleDbCommand(sql, con)
    cmd.ExecuteNonQuery()

    cmd.Dispose()
Next

MsgBox("save")
Me.Refresh()
0

@Shark_1 wow youre amazing! It works. thank you very much. :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.