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()

Recommended Answers

All 2 Replies

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()

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

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.