Results 1 to 4 of 4

Thread: [RESOLVED] datagridview update problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Resolved [RESOLVED] datagridview update problem

    i get this error when i click on my update button:
    Syntax error in UPDATE statement.

    my primary key is in column(0) but i hide this column from the user
    am i setting up my update command correctly?

    vb.net Code:
    1. Private cnxnString As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\SaturationTags.mdb")
    2.     Private cnxn As New System.Data.OleDb.OleDbConnection(cnxnString)
    3.     Private sql As New System.Data.OleDb.OleDbCommand
    4.     Private myDataAdapter As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [tblFields]", cnxn)
    5.     Private cmdBuilder As New System.Data.OleDb.OleDbCommandBuilder(myDataAdapter)
    6.     Private dbTable As New DataTable
    7.  
    8.     Public Sub frmDisplayFields_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    9.         myDataAdapter.Fill(dbTable)
    10.         With DataGridView1
    11.             .DataSource = dbTable
    12.             .AllowUserToAddRows = False
    13.             .AllowUserToDeleteRows = False
    14.             .AllowUserToOrderColumns = False
    15.             .AllowUserToResizeColumns = False
    16.             .AllowUserToResizeRows = False
    17.             .Columns(0).Visible = False
    18.             .Columns(1).ReadOnly = True
    19.             .Columns(2).Width = 200
    20.         End With
    21.     End Sub
    22.  
    23.     Private Sub cmdDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDone.Click
    24.         myDataAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand
    25.         myDataAdapter.Update(dbTable)
    26.     End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview update problem

    This has got nothing to with the DataGridView. The most common cause of this error is the use of a column name that is an SQL reserved word. The CommandBuilder doesn't wrap identifiers in brackets for you so a reserved word will cause a syntax error. Avoid using reserved words as column and table names if possible. If not, if you write out your query in full and use brackets yourself then I think the CommandBuilder should do the same.

    Also, get rid of this line:
    vb.net Code:
    1. myDataAdapter.UpdateCommand = cmdBuilder.GetUpdateCommand
    It's pointless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Re: datagridview update problem

    damn, your right. the field name i was trying to update was called "field" (bad choice for a field name). thanks a million. i probably wouldn't have figured that one out for a while.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview update problem

    Quote Originally Posted by bezaman
    damn, your right. the field name i was trying to update was called "field" (bad choice for a field name). thanks a million. i probably wouldn't have figured that one out for a while.
    It took me a while to figure it out when I first encountered the same thing four or five years ago. Glad to save someone else the same frustration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width