3 Replies - 4152 Views - Last Post: 05 June 2012 - 11:40 AM Rate Topic: -----

#1 jsj1411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-May 12

how to save records in list view into ms database table

Posted 05 June 2012 - 11:19 AM

i'm having problem saving my records into database
here's my code
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim con As New OleDbConnection
        con.ConnectionString = "Provider= microsoft.jet.oledb.4.0; data source = ..\Jualan.mdb"
        Dim cmd As New OleDbCommand()
        InitializeComponent()
        con.Open()
        For i As Integer = 0 To ListView1.Items.Count - 1
            Dim query As String = "INSERT INTO Jual VALUES (No_resit, Kod_produk, Nama_produk, Harga, Kuantiti, Tarikh, Sub_total)"
            cmd = New OleDbCommand(query, con)
            cmd.Parameters.Add("No_resit", OleDbType.[Integer]).Value = (ListView1.Items(i).SubItems(1).Text)
            '!!
            cmd.Parameters.Add("Kod_produk", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(2).Text
            cmd.Parameters.Add("Nama_produk", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(3).Text
            cmd.Parameters.Add("Harga", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(4).Text
            cmd.Parameters.Add("Kuantiti", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(5).Text
            '!!
            cmd.Parameters.Add("Tarikh", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(0).Text
            ' !!
            cmd.Parameters.Add("Sub_total", OleDbType.VarChar, 50).Value = ListView1.Items(i).SubItems(6).Text
            ' !!
            cmd.ExecuteNonQuery()
            con.Close()
        Next


        cmd.Dispose()

    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: how to save records in list view into ms database table

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14776
  • View blog
  • Posts: 59,069
  • Joined: 12-June 08

Re: how to save records in list view into ms database table

Posted 05 June 2012 - 11:28 AM

Quote

i'm having problem saving my records into database

Problems - they work best when explained, errors written out, and faulty lines pointed out.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: how to save records in list view into ms database table

Posted 05 June 2012 - 11:29 AM

You might be better off specifying the Fields and Values. Also in your parameters, specify them by prefixing @ in front of them.

I think you have confused the SQL interpreter way beyond anything other than a generic syntax error statement.


INSERT INTO Jual (No_resit, Kod_produk, Nama_produk, Harga, Kuantiti, Tarikh, Sub_total) VALUES (@No_resit, @Kod_produk, @Nama_produk, @Harga, @Kuantiti, @Tarikh, @Sub_total)
cmd.Parameters.Add("@No_resit", OleDbType.[Integer]).Value = (ListView1.Items(i).SubItems(1).Text)
and so on...

Of course you will need to fix your field names if they are different. I just copied what you had as an example of what I was talking about.
Was This Post Helpful? 0
  • +
  • -

#4 jsj1411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-May 12

Re: how to save records in list view into ms database table

Posted 05 June 2012 - 11:40 AM

no error when the codes in run , but no records are save in the database when the button is click
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1