INSERT sql runs fine but no data is entered into the DB

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
HI

I am trying to write data to an access table from vb.net. The code all seems to run fine but when i check the db no new data has been entered!

the code i have got is:

VB.NET:
Dim itemcount As Integer
        Dim MyCn As OleDbConnection
        Dim sql As String
        'Dim Value As String
        Dim Command As OleDbCommand
        Try

            con.Open()
            MyCn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
            For Each ListView In lstErrors.Items
                itemcount = itemcount + 1

                sql = "INSERT INTO CAllRates (CallRates.Destination) VALUE (" & ListView & ")"
                Command = New OleDbCommand(sql, MyCn)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message & " - " & ex.Source)
            con.Close()
        End Try

        lblcount.Text = itemcount
Dim itemcount As Integer
        Dim MyCn As OleDbConnection
        Dim sql As String
        'Dim Value As String
        Dim Command As OleDbCommand
        Try

            con.Open()
            MyCn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\BillAnalyst\ReferenceData.mdb")
            For Each ListView In lstErrors.Items
                itemcount = itemcount + 1

                sql = "INSERT INTO CAllRates (CallRates.Destination) VALUE (" & ListView & ")"
                Command = New OleDbCommand(sql, MyCn)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message & " - " & ex.Source)
            con.Close()
        End Try

        lblcount.Text = itemcount

I wondered if anyone had ideas what i was doing wrong

many thanks
 
Many obvious errors:
1. You need to MyCn.open() and MyCn.close rather than con.Open() and con.Close()
2. You don't add Command.Connection=MyCn
3. The insert statement has error: change VALUE to VALUES
4. You did not add Command.ExecuteNonQuery
 
Youre going wrong at the step where youre using an old, incorrect tutorial to teach you VB. Read the DW2 link in my signature, section "Creating a SImple Data App"
 

Latest posts

Back
Top