locked
Bindingsource AddNew() and CancelEdit() Interaction RRS feed

  • Question

  • I think I started out with a misunderstanding of what happens with addnew() and canceledit().

    I have a strongly typed datasource in memory.

    I call bindingsource.AddNew()

    Later I find that the record the user was trying to add already exists, so

    bindingsource.CancelEdit()

    I have not set any properties other than datasource on the bindingsource.

    My assumption was that the CancelEdit() would rollback the blank row added by the AddNew(), but I'm getting some odd errors which lead me to believe this is not true.

    Could someone verify that CancelEdit() leaves the new row?

    Wednesday, December 21, 2011 8:09 PM

Answers

  • <blockquote>

    1. The new item is added immediately to the internal list unless the data source implements the IEditableObject interface. In this case, the new item is not committed until an explicit call to ICancelAddNew.EndNew is made or until a new list operation is initiated. Before it is committed, the new item can be rolled back by calling CancelEdit, in which case the new item is discarded. </blockquote>

     

    taken from the addnew method here on msdn


    If you found my post helpful, please give me a recognition point on the left hand side of my post.
    • Proposed as answer by Mike Feng Friday, December 23, 2011 8:54 AM
    • Marked as answer by Mike Feng Thursday, December 29, 2011 1:52 PM
    Wednesday, December 21, 2011 10:59 PM
  • Samir: The actual error is immaterial, I'm getting a null error on a column that isn't null by inspection. That is unless the row I just called CancelEdit() on after AddNew() is being committed.

    Hi

    I believe your flow is like this, AddNew >> Save >> CancelEdit. or else you could not tell that this record already exist. Right? If so, CancelEdit will not work in this case, because CancelEdit is used before the row is committed. I usually use CancelEdit when I know the entry is wrong for some reason and the user changed his mind and he don't want to insert/edit any more before doing save.

    Once you saved, maybe you should use ResetCurrentItem? 

    • Proposed as answer by Mike Feng Friday, December 23, 2011 8:54 AM
    • Marked as answer by Mike Feng Thursday, December 29, 2011 1:52 PM
    Thursday, December 22, 2011 2:03 PM
  • I just figured it out, it hadn't occured to me that CancelEdit() would also fire PositionChanged in the calling program. Not a problem in itself, but I had canceled the parent and then the child. Canceling the parent ran PositionChanged while the child still existed, so I was getting the error on the child.

    It's amazing how a few days off can clear the mind.

    Thanks everybody for all the help!

    • Marked as answer by westfish Thursday, December 29, 2011 4:03 PM
    Thursday, December 29, 2011 4:03 PM

All replies

  • <blockquote> but I'm getting some odd errors which lead me to believe this is not true.</p>
    </blockquote><br/>

    Define "Odd errors"? What is the error?

    I don't think you want us to guess it :)?
    Wednesday, December 21, 2011 10:10 PM
  • <blockquote>

    1. The new item is added immediately to the internal list unless the data source implements the IEditableObject interface. In this case, the new item is not committed until an explicit call to ICancelAddNew.EndNew is made or until a new list operation is initiated. Before it is committed, the new item can be rolled back by calling CancelEdit, in which case the new item is discarded. </blockquote>

     

    taken from the addnew method here on msdn


    If you found my post helpful, please give me a recognition point on the left hand side of my post.
    • Proposed as answer by Mike Feng Friday, December 23, 2011 8:54 AM
    • Marked as answer by Mike Feng Thursday, December 29, 2011 1:52 PM
    Wednesday, December 21, 2011 10:59 PM
  • Samir: The actual error is immaterial, I'm getting a null error on a column that isn't null by inspection. That is unless the row I just called CancelEdit() on after AddNew() is being committed.

    ddday1991: Yes, I have read that. And from the other research I have done it looks like the CancelEdit() should have discarded the new row. So I m obviously misunderstanding something which is why I asked the question.

    Thursday, December 22, 2011 3:10 AM
  • Samir: The actual error is immaterial, I'm getting a null error on a column that isn't null by inspection. That is unless the row I just called CancelEdit() on after AddNew() is being committed.

    Hi

    I believe your flow is like this, AddNew >> Save >> CancelEdit. or else you could not tell that this record already exist. Right? If so, CancelEdit will not work in this case, because CancelEdit is used before the row is committed. I usually use CancelEdit when I know the entry is wrong for some reason and the user changed his mind and he don't want to insert/edit any more before doing save.

    Once you saved, maybe you should use ResetCurrentItem? 

    • Proposed as answer by Mike Feng Friday, December 23, 2011 8:54 AM
    • Marked as answer by Mike Feng Thursday, December 29, 2011 1:52 PM
    Thursday, December 22, 2011 2:03 PM
  • I have not saved it explicitly, but I think when I reposition the bindingsource it is implicitly trying to save the record which is causing the errors. 

    I think I am doing exactly what you describe, AddNew() and then when the user realizes the record already exists CancelEdit(). So, from what you are saying I can imply that this process should rollback the add leaving no row. This could very well mean something else I am doing is causing the problem. This is related to the code you helped me with before but I didn't include the code to complete (or cancel) the add. I'm going to look into it more next week, and if I still cannot figure out what is happening I'll add this code to the project I had shared and I'm sure then you will be able to tell me where I have gone astray.

    Friday, December 23, 2011 12:57 PM
  • Sorry, I got a little confused, it was Mike Feng who had helped me with the previous code.
    Friday, December 23, 2011 12:58 PM
  • Hi Westfish,

    Nice to see you again.

    Actually, the code "bindingsource.addnew()" is coded by yourself : http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/08a88a5a-a8aa-48da-a81a-07153f5f69c4

    Anyway, if you show me your progress now, I am still glad to help you out of this issue as possible as I can.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, December 26, 2011 8:49 AM
  • I just figured it out, it hadn't occured to me that CancelEdit() would also fire PositionChanged in the calling program. Not a problem in itself, but I had canceled the parent and then the child. Canceling the parent ran PositionChanged while the child still existed, so I was getting the error on the child.

    It's amazing how a few days off can clear the mind.

    Thanks everybody for all the help!

    • Marked as answer by westfish Thursday, December 29, 2011 4:03 PM
    Thursday, December 29, 2011 4:03 PM
  • Hi Westfish,

    Thank you for sharing this solution here.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, December 30, 2011 2:42 AM