Question help troubleshooting debug error

iamwoturnot

New member
Joined
Aug 12, 2010
Messages
3
Programming Experience
5-10
Greetings all.
I am currently working on an application that loads an .mdb databsase file (Microsoft Access) via openfiledialog and allows you to navigate through the records in the table and edit them when needed.
I am receiving an error I do not understand that doesn't suggest fixes when I try to commit the changes made to a record.

My form contains 3 panels. The first contains the labels and textboxes for the record data. Once I load the db, the fields are filled, as expected.
The second contains my navigati0on buttons, allowing to me to cycle through the records (the table rows and columns). The thrird contains my edit buttons, allowing me to edit the records.

All my navigation buttons are working correctly allowing me to cycle through each one. (next record, previous record, first record, last record)
The problem occurs when I attempt to commit changes after I have added a new record under my commit changes button.

My edit buttons are add new, commit changes, delete, and update.
Once I press add new, the feilds clear as expected, allowing me to enter new data in my text boxes, however, once I click commit changes, under the btnCommit stub, this is when my error occurs. The error is as follows:

System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217900
Message=Syntax error in INSERT INTO statement.
Source=Microsoft JET Database Engine

I have searched many forums in reference to this particular error number, each one saying something different, none of which I truly understand.
I have been self teaching vb.net for approximately 6 months now, and am trying to practice working with databases and connectionstrings and the like.

The line that gives me the error is as follows:

da.Update(ds, "Contacts")

I havn't tested the update and delete buttons as of yet for I am trying to tackle one thing at a time.

Here is my Public Class in it's entirety:

VB.NET:
Public Class Form1

    Dim inc As Integer
    Dim MaxRows As Integer
    Dim dbFileName As String
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

        Dim DidWork As Integer = OpenFD.ShowDialog()

        OpenFD.InitialDirectory = "C:\"
        OpenFD.Title = "Load Contacts"
        OpenFD.Filter = "Access(*.mdb)|*.mdb"
        OpenFD.ShowDialog()

        If DidWork = DialogResult.OK Then
            dbFileName = OpenFD.FileName
            LoadContacts()
        Else
            MsgBox("Are you sure you want to cancel?")
        End If

    End Sub

    Private Sub LoadContacts()

        con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0;Data Source =" + dbFileName
        con.Open()
        sql = "SELECT * FROM tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Contacts")
        con.Close()
        MaxRows = ds.Tables("Contacts").Rows.Count
        inc = -1
        If inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        End If

    End Sub

    Private Sub NavigateRecords()

        txtContact.Text = ds.Tables("Contacts").Rows(inc).Item(1)
        txtFirstMail.Text = ds.Tables("Contacts").Rows(inc).Item(2)
        txtSecondMail.Text = ds.Tables("Contacts").Rows(inc).Item(3)
        txtLiveid.Text = ds.Tables("Contacts").Rows(inc).Item(4)
        txtYahooid.Text = ds.Tables("Contacts").Rows(inc).Item(5)
        txtAimid.Text = ds.Tables("Contacts").Rows(inc).Item(6)
        txtIcqid.Text = ds.Tables("Contacts").Rows(inc).Item(7)
        txtSkypeid.Text = ds.Tables("Contacts").Rows(inc).Item(8)
        txtRealname.Text = ds.Tables("Contacts").Rows(inc).Item(9)
        txtPhone.Text = ds.Tables("Contacts").Rows(inc).Item(10)

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

        If inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("No more records available.")
        End If

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        If inc > 0 Then
            inc = inc - 1
            NavigateRecords()
        ElseIf inc = -1 Then
            MsgBox("No records yet.")
        ElseIf inc = 0 Then
            MsgBox("First record.")
        End If

    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click

        If inc <> 0 Then
            inc = 0
            NavigateRecords()
        End If

    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click

        If inc <> MaxRows - 1 Then
            inc = MaxRows - 1
            NavigateRecords()
        End If

    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click

        btnCommit.Enabled = True
        btnAddNew.Enabled = False
        btnUpdate.Enabled = False
        btnDelete.Enabled = False
        txtContact.Clear()
        txtFirstMail.Clear()
        txtSecondMail.Clear()
        txtLiveid.Clear()
        txtYahooid.Clear()
        txtAimid.Clear()
        txtIcqid.Clear()
        txtSkypeid.Clear()
        txtRealname.Clear()
        txtPhone.Clear()

    End Sub

    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click

        If inc <> -1 Then

            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow

            dsNewRow = ds.Tables("Contacts").NewRow()

            dsNewRow.Item("Contact") = txtContact.Text
            dsNewRow.Item("Email1") = txtFirstMail.Text
            dsNewRow.Item("Email2") = txtSecondMail.Text
            dsNewRow.Item("Live ID") = txtLiveid.Text
            dsNewRow.Item("Yahoo ID") = txtYahooid.Text
            dsNewRow.Item("AIM ID") = txtAimid.Text
            dsNewRow.Item("ICQ ID") = txtIcqid.Text
            dsNewRow.Item("Skype ID") = txtSkypeid.Text
            dsNewRow.Item("Real Name") = txtRealname.Text
            dsNewRow.Item("Phone") = txtPhone.Text

            ds.Tables("Contacts").Rows.Add(dsNewRow)

            da.Update(ds, "Contacts")

            MsgBox("New Record added to the Database")

            btnCommit.Enabled = False
            btnAddNew.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("Contacts").Rows(inc).Delete()
        MaxRows = MaxRows - 1

        inc = 0
        NavigateRecords()
        da.Update(ds, "Contacts")

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        If txtContact.Text = "" Then
            MsgBox("You must atleast enter a contact handle inorder to update.")
        Else
            ds.Tables("Contacts").Rows(inc).Item(1) = txtContact.Text
            ds.Tables("Contacts").Rows(inc).Item(2) = txtFirstMail.Text
            ds.Tables("Contacts").Rows(inc).Item(3) = txtSecondMail.Text
            ds.Tables("Contacts").Rows(inc).Item(4) = txtLiveid.Text
            ds.Tables("Contacts").Rows(inc).Item(5) = txtYahooid.Text
            ds.Tables("Contacts").Rows(inc).Item(6) = txtAimid.Text
            ds.Tables("Contacts").Rows(inc).Item(7) = txtIcqid.Text
            ds.Tables("Contacts").Rows(inc).Item(8) = txtSkypeid.Text
            ds.Tables("Contacts").Rows(inc).Item(9) = txtRealname.Text
            ds.Tables("Contacts").Rows(inc).Item(10) = txtPhone.Text
            da.Update(ds, "Contacts")
            MsgBox("Data updated.")
        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        btnCommit.Enabled = False
        btnAddNew.Enabled = True
        btnUpdate.Enabled = True
        btnDelete.Enabled = True
        inc = 0
        NavigateRecords()

    End Sub
End Class

Any advice or assistance for this .NET newb would be greatly appreciated.
Thanks in advance.
 
Would you mind posting the INSERT statement in use?

I think it's probably because you've called one of your columns some reserved word (like "date", "type" etc)

-
Also, the other thing you can try.. Take a read of the DW4 link in my signature section "Creating a Simple Data App"
Just start a new project, it should take about 15 minutes to complete the tutorial, doesnt have to be pretty; just do your data access the microsoft recommended way and see if you still get the problem.
If the problem went away, consider updating the way you do data access.. As you will see when you follow the tutorial, how you have things here is purely making your own life hard work! :)
If the problem persists, you just found a bug that Microsoft will want to hear about.. ;)
 
Back
Top