1

i'm a newbie in programming vb.net, I'm making a simple point of sale I want to insert multiple rows of data from listview to linq to sql, here is the code:

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

    Dim db As New PenjualanDataContext
    For Each item As ListViewItem In ListView1.SelectedItems
        Dim TransaksiBaru As New Transaksi With {.Dibayar = Dibayar.Text, .Faktur = FakturTextBox.Text, .Harga = HargaComboBox.Text, .Jumlah = JumlahTextBox.Text, .Kembali = Kembali.Text, .Kode_Barang = Kode_BarangComboBox.Text, .Nama_Barang = Nama_BarangComboBox.Text, .SubTotal = SubTotalTextBox.Text, .Tanggal = TanggalDateTimePicker.Text, .Total = TotalTextBox.Text}
        db.Transaksis.InsertOnSubmit(TransaksiBaru)
        db.SubmitChanges()
    Next

End Sub

I cannot save the data to linq to sql.....

1
  • I cannot save the data to linq to sql. That can mean anything. Please be more specific. Commented Apr 2, 2017 at 11:55

1 Answer 1

2

First, it is faster to Submit Changes one time when all record are inserted in the context.

Second, it is recommended to use Using method

Third, you are looping over ListView1.SelectedItems and not using item object returned by the loop. i think that you are missing something in your code

Try modifying your code to the following:

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

    Using db As New PenjualanDataContext

        For Each item As ListViewItem In ListView1.SelectedItems

            Dim TransaksiBaru As New Transaksi With {.Dibayar = Dibayar.Text, .Faktur = FakturTextBox.Text, .Harga = HargaComboBox.Text, .Jumlah = JumlahTextBox.Text, .Kembali = Kembali.Text, .Kode_Barang = Kode_BarangComboBox.Text, .Nama_Barang = Nama_BarangComboBox.Text, .SubTotal = SubTotalTextBox.Text, .Tanggal = TanggalDateTimePicker.Text, .Total = TotalTextBox.Text}

            db.Transaksis.InsertOnSubmit(TransaksiBaru)

        Next

         db.SubmitChanges()

     End Using

End Sub

You can refer to the following MSDN article to read more about inserting Rows using Linq-to-SQL

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

3 Comments

To OP: You should be populating properties of Transaksi from item..
Wow, it works, thanks Hadi, you' re a big help!!! And if I want to print the listview data, can I be able to use Using Method also?
Happy to help. Yes you can use it. If this answer worked you have to accept it else give me a feedback

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.