Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Help: Dataset not updating database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Resolved [RESOLVED] [2005] Help: Dataset not updating database

    I'm using a typed dataset to retrieve data from an access db. The retrieval works fine; the disconnected datatable updates fine but the dataset does not update the database when I use adapter.dataset.update function. Please help. Neither works.

    VB Code:
    1. Dim myDataRow As DataRow
    2.  
    3. myDataRow = Me.DsDefault.tb_users.NewRow()
    4. myDataRow("fname") = strFname
    5. myDataRow("lname") = strLname
    6. myDataRow("phone") = strPhone
    7. myDataRow("address") = strAddress
    8. myDataRow("city") = strCity
    9. myDataRow("current_flag") = intFlag
    10. myDataRow("state") = strState
    11. myDataRow("zip") = strZip
    12. myDataRow("fax") = strFax
    13. myDataRow("contact") = strContact
    14. myDataRow("date_added") = strDateAdded
    15.  
    16. Me.DsDefault.tb_users.Rows.Add(myDataRow)
    17. Debug.WriteLine("After Add Row: " & myDataRow.RowState.ToString)
    18.  
    19. Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users)
    20.  
    21.  
    22. '======Update datarow routine ========
    23. Dim myDataRow As DataRow = Me.DsDefault.tb_users.Rows(intRow)
    24.  
    25. myDataRow = Me.DsDefault.tb_users.NewRow()
    26. myDataRow("fname") = strFname
    27. myDataRow("lname") = strLname
    28. myDataRow("phone") = strPhone
    29. myDataRow("address") = strAddress
    30. myDataRow("city") = strCity
    31. myDataRow("current_flag") = intFlag
    32. myDataRow("state") = strState
    33. myDataRow("zip") = strZip
    34. myDataRow("fax") = strFax
    35. myDataRow("contact") = strContact
    36. myDataRow("date_added") = strDateAdded
    37.  
    38. Debug.WriteLine("After Edit Row: " & myDataRow.RowState.ToString)
    39.  
    40. Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users)

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

    Re: [2005] Help: Dataset not updating database

    Your alleged Update routine is not updating any row. You get an existing row first but then you create a new row and set its fields instead. I'm guessing that you've copied and pasted and forgotten to delete that row.
    VB Code:
    1. '======Update datarow routine ========
    2. Dim myDataRow As DataRow = Me.DsDefault.tb_users.Rows(intRow) '<- This gets an existing row.
    3.  
    4. myDataRow = Me.DsDefault.tb_users.NewRow() '<- This creates a new row so the reference to the existing row is lost.
    All the subsequent code is setting the fields of the new row, which never gets added to the table, thus there are no changed rows for the Update call to affect.
    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
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Angry Re: [2005] Help: Dataset not updating database

    Thanks for your response jmcilhinney. The code I posted are 2 different sub-routines. The Dataset gets updated but doesn't update the database. Is there anything I'm doing wrong? Please help.

    VB Code:
    1. AddNewUser() '<<< Doesn't work
    2. UpdateOldUser(2) '<<<Doesn't work either.
    3.  
    4. Private Sub AddNewUser()
    5. Dim myDataRow As DataRow
    6.  
    7. myDataRow = Me.DsDefault.tb_users.NewRow()
    8. myDataRow("fname") = strFname
    9. myDataRow("lname") = strLname
    10. myDataRow("phone") = strPhone
    11. myDataRow("address") = strAddress
    12. myDataRow("city") = strCity
    13. myDataRow("current_flag") = intFlag
    14. myDataRow("state") = strState
    15. myDataRow("zip") = strZip
    16. myDataRow("fax") = strFax
    17. myDataRow("contact") = strContact
    18. myDataRow("date_added") = strDateAdded
    19.  
    20. Me.DsDefault.tb_users.Rows.Add(myDataRow)
    21. Debug.WriteLine("After Add Row: " & myDataRow.RowState.ToString)
    22.  
    23. Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users)
    24. End Sub
    25.  
    26. Private Sub UpdateOldUser(ByVal intRow As Integer)
    27.  
    28. '======Update datarow routine ========
    29. Dim myDataRow As DataRow = Me.DsDefault.tb_users.Rows(intRow)
    30.  
    31. myDataRow = Me.DsDefault.tb_users.NewRow()
    32. myDataRow("fname") = strFname
    33. myDataRow("lname") = strLname
    34. myDataRow("phone") = strPhone
    35. myDataRow("address") = strAddress
    36. myDataRow("city") = strCity
    37. myDataRow("current_flag") = intFlag
    38. myDataRow("state") = strState
    39. myDataRow("zip") = strZip
    40. myDataRow("fax") = strFax
    41. myDataRow("contact") = strContact
    42. myDataRow("date_added") = strDateAdded
    43.  
    44. Debug.WriteLine("After Edit Row: " & myDataRow.RowState.ToString)
    45.  
    46. Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users)
    47. End Sub

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

    Re: [2005] Help: Dataset not updating database

    Well, the second one can't possibly work for the reason I have already stated. Why do you say it doesn't work? Have you done a query afterwards to see if the data is there, or are you exiting the application and then finding it's not there when you restart? The simple way to know whether it is working or not is to use the Update method. It is not a procedure, but rather a function. That means that it returns a value, and the value it returns is the number of rows affected by the call. You can simply do this:
    VB Code:
    1. MessageBox.Show(Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users).ToString())
    A message will popup telling you how many rows were deleted, inserted or updated. If that message says zero then you haven't saved any changes, otherwsie you have. Note that a more professional way to do it would be something like this:
    VB Code:
    1. Trace.WriteLine(Me.tb_usersTableAdapter.Update(Me.DsDefault.tb_users) & " rows affected")
    Using this code the app will behave normally when released but while debugging it will write a message to the Immediate window telling you how many rows were affected.
    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

  5. #5
    Lively Member
    Join Date
    Jun 2006
    Location
    Florida
    Posts
    78

    Re: [2005] Help: Dataset not updating database

    I have found that if you are debugging the application the database will not update. To acctually see the database get updated you need to run the application outside of VS.

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

    Re: [2005] Help: Dataset not updating database

    Quote Originally Posted by rfiddelke
    I have found that if you are debugging the application the database will not update. To acctually see the database get updated you need to run the application outside of VS.
    That is absolutely not true.
    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

  7. #7
    Lively Member
    Join Date
    Jun 2006
    Location
    Florida
    Posts
    78

    Re: [2005] Help: Dataset not updating database

    I think this only happens with SQL express. When debugging it runs against a copy of the database. I know I was having a similar problem and that was the issue. Sorry for not being more specific.

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

    Re: [2005] Help: Dataset not updating database

    Quote Originally Posted by rfiddelke
    I think this only happens with SQL express. When debugging it runs against a copy of the database. I know I was having a similar problem and that was the issue. Sorry for not being more specific.
    It absolutely does run against a copy of the database, whether you're debugging or in release. When you compile the database file is copied to the output folder along with the compiled assembly. You'd hardly want to be saving data to your original database would you? The default behaviour is for the database to be copied to the output folder every time you compile, which means every time you run the run the project in the debugger. When you save data to the database it IS saved, but if you exit the app and then run it again your database will be overwritten with a new clean copy. People seem to just assume that this means that the save didn't work without checking whether it worked at the time. You can change this default behaviour in the Properties window for the database so that it will only be copied to the output folder if the origianl has changed or not at all. In my opinion the default should have been only when it has changed, but you can't have everything.
    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

  9. #9
    New Member
    Join Date
    Jun 2006
    Location
    Italy
    Posts
    1

    Re: [2005] Help: Dataset not updating database

    I have a similar Problem using Vs2005 ....and an access Database using a table adapter and update .... can you help?

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

    Re: [2005] Help: Dataset not updating database

    Quote Originally Posted by Begineeralbert
    I have a similar Problem using Vs2005 ....and an access Database using a table adapter and update .... can you help?
    It's the same exact situation. The database is being overwritten each time you compile. If this is an issue then change the properties of the database. You can confirm that your data is actually being saved using the code I posted earlier, or simply run a query straight after updating and see what the database contains.
    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Re: [2005] Help: Dataset not updating database

    what do you mean by changing the properties of the database? What should I change? and to what should I change it to?

    Thanks

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Re: [2005] Help: Dataset not updating database

    Got it. You were right about it copying the database temporarily.

    To Begineeralbert: Right-clicked on the access db within the solution explorer click on property. Set the Copy to output to copy if newer.

    Thanks jmcilhinney.

  13. #13
    Member
    Join Date
    May 2006
    Posts
    57

    Re: [2005] Help: Dataset not updating database

    i have this problem with a paremterized query like
    select * from table where field =?
    The record loads fine, but after modifying the value in the textboxes
    of the form and click save in the bindingnavigator:

    bindingnavigator.endedit()
    tableadapter.udpdate(dataset)

    in the last sentence i get
    "no valid update command" error

    the dataset marks the row as modified
    but if i use
    dataset.acceptchanges
    the modify flag is reset but the value
    in the database is not changed ( i open the database in access and look the record)

  14. #14
    Member
    Join Date
    May 2006
    Posts
    57

    Re: [2005] Help: Dataset not updating database

    i have this problem with a paremterized query like
    select * from table where field =?
    The record loads fine, but after modifying the value in the textboxes
    of the form and click save in the bindingnavigator:

    bindingnavigator.endedit()
    tableadapter.udpdate(dataset)

    in the last sentence i get
    "no valid update command" error

    the dataset marks the row as modified
    but if i use
    dataset.acceptchanges
    the modify flag is reset but the value
    in the database is not changed ( i open the database in access and look the record)

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