dcsimg
Results 1 to 24 of 24

Thread: OLEDB Dataadaptor not updating on first request

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    OLEDB Dataadaptor not updating on first request

    Hi

    I have a strange situation! I have a VB form which saves to an Access database. The form has been working fine until recently.

    When I save the record, the first dataadaptor update does not save the record. No error is displayed.

    If I click the save record again, the record saves.

    Here is my code for the save button :-

    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    form_validate()

    If form_ok = True Then
    cu_sale.EndCurrentEdit()
    Dim cb As New OleDb.OleDbCommandBuilder(da_sale)
    Try
    da_sale.Update(ds_sale, "Sales")
    MsgBox("Record Saved")
    refresh_dataset()
    open_mode()

    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End If
    End Sub

    Any advice greatly received!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    Call the form's Validate method. I came to realise some time ago that EndCurrentEdit is not necessarily enough to force pending changes to be committed.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by jmcilhinney View Post
    Call the form's Validate method. I came to realise some time ago that EndCurrentEdit is not necessarily enough to force pending changes to be committed.
    Thanks for your reply. Excuse my knowledge.....How do I do that?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by neilq View Post
    Thanks for your reply. Excuse my knowledge.....How do I do that?
    I have tried me.validate and it still does not update first time.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    Show us the new code. Also, what value does the Update call return?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    This is the new code :-
    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    form_validate()

    If form_ok = True Then
    me.Validate()
    cu_sale.EndCurrentEdit()
    Dim cb As New OleDb.OleDbCommandBuilder(da_sale)
    Try
    da_sale.Update(ds_sale, "Sales")
    MsgBox("Record Saved")
    refresh_dataset()
    open_mode()

    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End If
    End Sub

    What code do I use to get the Update call return?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    You don't know how to get the value returned by a function?

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    No, not sure.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    That's one of the most fundamental things you can do in programming so, if you can't do that, you can't really do much of anything. I suggest that you take some time to learn the basics. There's a link to a tutorial in my signature below and it will cover how to get the value returned by a function, among other things. I suggest that you take the time to work through it.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by jmcilhinney View Post
    That's one of the most fundamental things you can do in programming so, if you can't do that, you can't really do much of anything. I suggest that you take some time to learn the basics. There's a link to a tutorial in my signature below and it will cover how to get the value returned by a function, among other things. I suggest that you take the time to work through it.
    The command builder is returning a valid sql insert statement. When I save the record again, the statement is the same and the record saves.

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    When you post code put it between code tags, click "#" then insert your code

    Code:
    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
    form_validate()
    
    If form_ok = True Then
    me.Validate()
    cu_sale.EndCurrentEdit()
    Dim cb As New OleDb.OleDbCommandBuilder(da_sale)
    Try
    da_sale.Update(ds_sale, "Sales")
    MsgBox("Record Saved")
    refresh_dataset()
    open_mode()
    
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End If
    End Sub
    You say it doesn't update the first time. Does the "Record Saved" message display the first time? If it doesn't and there's no error then "form_ok" is probably False.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    It does show "Record Saved" first time and second time when it actually saves the record.

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    I'm confused as to why your using EndCurrentEdit, what is "cu_sale"?

    When jmc talks about what does the update return, da_sale.Update(ds_sale, "Sales") will return a value indicating how many records were modified.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by wes4dbt View Post
    I'm confused as to why your using EndCurrentEdit, what is "cu_sale"?
    It will be a CurrencyManager, which is one of the things that gets wrapped up in a BindingSource. You should use a BindingSource and call EndEdit today but, prior to .NET 2.0, that wasn't an option.

  15. #15
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,057

    Re: OLEDB Dataadaptor not updating on first request

    Two things...

    Firstly where is form_ok being set to True?

    Secondly have you tried putting a breakpoint on the method and stepping through the code to see if it is actually doing what you intend to do? If it does something unexpected then use the debugger to inspect the variables etc. and figure out why it is behaving differently to your expectations.

  16. #16
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by jmcilhinney View Post
    It will be a CurrencyManager, which is one of the things that gets wrapped up in a BindingSource. You should use a BindingSource and call EndEdit today but, prior to .NET 2.0, that wasn't an option.
    Yeah, I've seen EndCurrentEdit with BindingContext and CurrencyManager but didn't understand why either one of them would be used here. Didn't do much .Net programming in the 2.0 days, thanks for the information.

  17. #17

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by PlausiblyDamp View Post
    Two things...

    Firstly where is form_ok being set to True?

    Secondly have you tried putting a breakpoint on the method and stepping through the code to see if it is actually doing what you intend to do? If it does something unexpected then use the debugger to inspect the variables etc. and figure out why it is behaving differently to your expectations.
    Form_ok is a procedure that checks all fields have been completed, it is returning true.

    I have tried a breakpoint and inspected variables, there is nothing peculiar with the variables.

    The save procedure steps through the code and prompts that the record has been saved, on checking the database the record has not been saved. With the form open the save procedure runs the same code and then writes to the database.

    I have other forms in the application that use the same code (albeit different datasets and data adaptors) and the save record code works first time.

  18. #18
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    Have you checked the return value from "da_sale.Update(ds_sale, "Sales")"? If so, what happens each time?

  19. #19

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by wes4dbt View Post
    Have you checked the return value from "da_sale.Update(ds_sale, "Sales")"? If so, what happens each time?
    I have, on both occassions it returns 0, however the database updates on the second request.

  20. #20
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    I don't see how that's possible. I'm stumped, good luck.

  21. #21
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by neilq View Post
    I have, on both occassions it returns 0, however the database updates on the second request.
    Both occasions? I don't see how that's possible either. Update returns the number of records affected by the operation so 0 means no affected records. If Update returns 0 and yet a record is affected then something is broken on your system.

  22. #22
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,151

    Re: OLEDB Dataadaptor not updating on first request

    If the Adapter really does return 0 every time and the system isn't broken then the only other thing I can think of is that the database is being updated somewhere else in the code.

  23. #23

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    10

    Re: OLEDB Dataadaptor not updating on first request

    Quote Originally Posted by wes4dbt View Post
    I don't see how that's possible. I'm stumped, good luck.
    Apologies, my last reply was incorrect. On clicking save record the first time the data adaptor returns 0 records updated. On clicking the save record again it then returns 1 record updated and saves the record.

    I have stepped through the code and on the first attempt at saving the record the update code is stepped through.

  24. #24
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,575

    Re: OLEDB Dataadaptor not updating on first request

    Then your DataTable contains no modified records at that time. I would have thought that Validate and EndCurrentEdit would do it but maybe it's the wrong CurrencyManager or something like that. Not sure if you can make changes like this but, ideally, you'd bind the DataTable to the UI via a BindingSource and then Validate and EndEdit on that BindingSource should do the trick and have in every scenario I've seen.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width