+ Reply to Thread
Results 1 to 4 of 4

Thread: Syntax error in INSERT INTO statement

  1. #1
    Versaiteis is offline VB.NET Forum Newbie Versaiteis is on a distinguished programming path ahead
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    18
    Posts
    14
    Reputation
    7

    Default Syntax error in INSERT INTO statement

    Alright, I admit that I have a similar post in the MS Access section, but I feel that the problem that I am having relies between the data adapter and the command builder =\

    da=data adapter
    ds=dataset

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = Single.mdb"
    
            con.Open()
            sql = "SELECT * FROM tblClients"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "AddressBook")
            con.Close()
    
    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
        
                MsgBox(cb.GetInsertCommand.CommandText)
                da.InsertCommand = cb.GetInsertCommand
                da.UpdateCommand = cb.GetUpdateCommand
                da.DeleteCommand = cb.GetDeleteCommand
    
                dsNewRow = ds.Tables("AddressBook").NewRow()
    
                dsNewRow.Item(0) = 3
                dsNewRow.Item(1) = txtFirstName.Text
                dsNewRow.Item(2) = Date.Now
    
                ds.Tables("AddressBook").Rows.Add(dsNewRow)
    
                da.Update(ds, "AddressBook")
    
                MsgBox("New Record added to Database")
    
                btnCommit.Enabled = False
                btnAddNew.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
            End If
        End Sub
    The Insert command that the message box displays looks fine, I can see no syntax errors in it, and yet the program crashes on the update line
    Code:
    da.update(ds, "AddressBook")
    I will be more than happy to provide any other needed information, I am quite at a loss for what to do next. This problem has been a thorn in my side for a while -_-

  2. #2
    Versaiteis is offline VB.NET Forum Newbie Versaiteis is on a distinguished programming path ahead
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    18
    Posts
    14
    Reputation
    7

    Default Resolved

    The solution was found.

    Where this was my SQL statement:

    Code:
    INSERT INTO tblClients (First Name, Last Name) VALUES (?,?)
    The correct statement was:

    Code:
    INSERT INTO tblClients (First_Name, Last_Name) VALUES (?,?)

  3. #3
    ggunter is offline VB.NET Forum Enthusiast ggunter is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2008
    Posts
    88
    Reputation
    37

    Default

    Just an FYI...

    Pretty sure you could also have just enclosed the field names in brackets.

    Code:
    INSERT INTO tblClients ([First Name], [Last Name]) VALUES (?,?)
    That should force them to be recognized as field names.

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute cjard has a reputation beyond repute
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2006
    Age
    66
    Posts
    6,676
    Reputation
    927

    Default

    yeah but that wouldnt have helped if his column names actually contained underscores!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts