Results 1 to 7 of 7

Thread: Issues with saving changes to database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    240

    Issues with saving changes to database

    I have buttons working that cycles through all the records and everything shows up fine. I also made a simple DB program to test what I was doing without all the extra stuff my current program has and it works fine. Updates the dataset and then saves to the database and reflects the changes. When I tried adding it to this program, I get errors when I click "Save" and "Commit".

    The "Save" (updates DS) error is: "Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

    The "Commit" (commits changes to DB) error is: "Additional information: Syntax error in INSERT INTO statement."

    Not sure what either of these mean, the same code worked fine in the standalone program. Looking at another site, they show the exact Save error I'm getting and say it's because I don't have a command builder, but it's up there. Here is all relevant code to the DB stuff:

    vb Code:
    1. '''Testing DB functionality
    2.     Dim inc As Integer
    3.     Dim MaxRows As Integer
    4.  
    5.     Dim con As New OleDb.OleDbConnection
    6.     Dim ds As New DataSet
    7.     Dim data As OleDb.OleDbDataAdapter
    8.     Dim sql As String
    9.  
    10.      con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\work\EyeBase.mdb"
    11.         con.Open()
    12.  
    13.  
    14.         sql = "SELECT * FROM History"
    15.         data = New OleDb.OleDbDataAdapter(sql, con)
    16.         data.Fill(ds, "History")
    17.  
    18.         con.Close()
    19.  
    20.  
    21.         MaxRows = ds.Tables("History").Rows.Count
    22.         inc = -1
    23.  
    24. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    25.         If inc <> MaxRows - 1 Then
    26.             inc = inc + 1
    27.             NavigateRecords()
    28.         Else
    29.             MsgBox("No More Rows")
    30.         End If
    31.     End Sub
    32.  
    33.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    34.         If inc <> MaxRows - 1 Then
    35.             inc = MaxRows - 1
    36.             NavigateRecords()
    37.         End If
    38.     End Sub
    39.  
    40.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    41.         If inc <> 0 Then
    42.             inc = 0
    43.             NavigateRecords()
    44.         End If
    45.     End Sub
    46.  
    47.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    48.         If inc > 0 Then
    49.             inc = inc - 1
    50.             NavigateRecords()
    51.         ElseIf inc = -1 Then
    52.             MsgBox("No Records Yet")
    53.         ElseIf inc = 0 Then
    54.             MsgBox("First Record")
    55.         End If
    56.     End Sub
    57.  
    58.  
    59.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    60.         If inc = -1 Then
    61.             MsgBox("Can't update yet")
    62.         Else
    63.  
    64.  
    65.             ds.Tables("History").Rows(inc).Item(3) = Chief_ComplaintTextBox.Text
    66.             'ds.Tables("AddressBook").Rows(inc).Item(2) = txtLast.Text
    67.             'ds.Tables("AddressBook").Rows(inc).Item(3) = txtAddress1.Text
    68.             'ds.Tables("AddressBook").Rows(inc).Item(4) = txtAddress2.Text
    69.             'ds.Tables("AddressBook").Rows(inc).Item(5) = txtAddress3.Text
    70.             'ds.Tables("AddressBook").Rows(inc).Item(6) = txtZIP.Text
    71.             'ds.Tables("AddressBook").Rows(inc).Item(7) = txtPhone.Text
    72.             'ds.Tables("AddressBook").Rows(inc).Item(8) = txtEmail.Text
    73.             'ds.Tables("AddressBook").Rows(inc).Item(9) = txtNotes.Text
    74.  
    75.             data.Update(ds, "History")
    76.  
    77.             MsgBox("Data updated")
    78.         End If
    79.     End Sub
    80.  
    81.     Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
    82.         If inc <> -1 Then
    83.  
    84.             Dim cb As New OleDb.OleDbCommandBuilder(data)
    85.             Dim dsNewRow As DataRow
    86.  
    87.             dsNewRow = ds.Tables("History").NewRow()
    88.  
    89.             dsNewRow.Item("Chief Complaint") = Chief_ComplaintTextBox.Text
    90.             'dsNewRow.Item("Surname") = txtLast.Text
    91.             'dsNewRow.Item("Address1") = txtAddress1.Text
    92.             'dsNewRow.Item("Address2") = txtAddress2.Text
    93.             'dsNewRow.Item("Address3") = txtAddress3.Text
    94.             'dsNewRow.Item("Postcode") = txtZIP.Text
    95.             'dsNewRow.Item("Phone") = txtPhone.Text
    96.             'dsNewRow.Item("Email") = txtEmail.Text
    97.             'dsNewRow.Item("Notes") = txtNotes.Text
    98.  
    99.             ds.Tables("History").Rows.Add(dsNewRow)
    100.  
    101.             data.Update(ds, "History")
    102.  
    103.             MsgBox("New Record added to the Database")
    104.         End If
    105.     End Sub

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Issues with saving changes to database

    The errors are telling you exactly what the problem are. For the 1st one, you need to provide a valid update query to the UpdateCommnad object. With the 2nd error, you have to examine your insert query to see where the syntax error is and fix it.
    I highly recommend you to learn more about ADO.Net. There are a lot of tutorials on that on this site as well as on the internet.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    240

    Re: Issues with saving changes to database

    I also read that you can get the Syntax Error if a column in your table is a reserved word in the Jet 4.0 reserved words. One of the columns is named Date and that's a reserved word, so I wasn't sure if that was the problem for that.

    I would also like to know why code that works fine in another program doesn't work in this one. I thought if I renamed things to match this program and made sure I had the row information correct that it would work. It scrolls through fine using the other code, but the save and update won't work.

    I have the data.Update in there, which I thought was supposed to add those commands. As far as my insert query, I assumed that if it was pulling data from every column fine that it was set up correctly, I had it insert every column in the table.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Issues with saving changes to database

    Reserved words do cause problems like that, so you should change the field/table names that are reserved words (in any part of the process, not just Jet).

    For more information (including links to lists of reserved words), see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    240

    Re: Issues with saving changes to database

    Quote Originally Posted by si_the_geek View Post
    Reserved words do cause problems like that, so you should change the field/table names that are reserved words (in any part of the process, not just Jet).

    For more information (including links to lists of reserved words), see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
    Thanks. Only problem is it won't let me change the name of the column because the table is linked somewhere. I'll have to figure that out.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Issues with saving changes to database

    Yes, that's the problem. As a rule of thumb, never name a column using a reserved key word. If you don't have any control over that (like, it's not your database or changing it will break other applications that also use that database), you wrap the column name in square bracket, something like
    Code:
    Select [Date], userId from tablename
    A data adapter has 3 command objects: insert, update and delete. If your table has a primary key, the data adapter will try to generate the insert, update and delete queries automatically based on that primary key. It knows if a table has a primary key or not from the select query. For any reason that it cannot generate those commands automatically, you will have to provide them yourself. It's just a matter of supplying the right insert, update and delete SQL statement to the command objects.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    240

    Re: Issues with saving changes to database

    Yep, I didn't create this database.

    So, for this line:

    Code:
      data.Update(ds, "History")
    Would I have to get rid of the Update portion and declare a new variable with my custom insert, update, and delete statements so I could reference it, or is it done a different way?

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