.NewRow() Error - An Item with the same key has already been added.


Author
Message
jsantos
jsantos
StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)
Group: StrataFrame Users
Posts: 26, Visits: 87
Hello,

I am having an issue when calling .NewRow I get an error in the CreateShareAndAddBusinessObject function in BusinessBindingSource.vb (see attached image)

Before calling the .NewRow in our code the CurrentRowIndex = -1 which I expect as the BO should be empty. Then as soon as I call .NewRow I get an exception. When the error is thrown index = 0 when it should be 1 according to loReturn where it looks like there is already one record and then the error makes sense.

Any ideas?

http://forum.strataframe.net/Uploads/Images/c855b057-bc96-4ba1-a242-0e50.png
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi JSantos,

I your screenshot is not quite visible, it is always best to add it as attachment so we can actually see what you want us to see.

Now back to your issue, it looks like you are trying to add a new record to an SF BusinessBindingSource (BBS), if that is the case, you simply call the Add() method in the BO bind to the BBS and the new record will show up in your control.

If I am wrong, please clarify what you are trying to accomplish.

Edhy Rijo

jsantos
jsantos
StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)
Group: StrataFrame Users
Posts: 26, Visits: 87
Sorry about the image. Added attachment

We are adding a new row programatically. As I understand .NewRow is recommended for programatically adding new rows.
Attachments
Error1.png (239 views, 53.00 KB)
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi,

Thanks for the image and yes, NewRow() is recommended for adding rows programmatically.

Still, can you please explain in a wider scope what you are trying to do?  also it will help if you can post the code Sub or Function where you are trying to add the new record?

I use BBS with DevExpress all the time and have notice that when showing records in the DevEx grid using the BBS, and using the same source BO to modify the record, all kind of error show up.  My solution is to use 2 BO, one for the BBS to show in read only grid and one for make the changes.  I know this logic will require more code to keep the grid in synch with updates to 2nd BO, but that have worked for me all the time.

Edhy Rijo

jsantos
jsantos
StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)
Group: StrataFrame Users
Posts: 26, Visits: 87
I have attached a screenshot of the code that I think you will see where it is happening.

Thanks for your help.
Attachments
Code1.png (225 views, 45.00 KB)
jsantos
jsantos
StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)
Group: StrataFrame Users
Posts: 26, Visits: 87
I have also just tried using .Add and I get the same error in the same location CreateShareAndAddBusinessObject.
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Howdy J,

The CreateShareAndAddBusinessObject() method is called from within the Get of the Item() property on BusinessBindingSource.  In the Get, it calls Not _BusinessObjectList.TryGetValue(index, loReturn), which effectively tests whether the key is contained in the dictionary and returns the value.  So, if the Me.CreateShareAndAddBusinessObject(index) is being called, then the specified index does not exist within the dictionary.  

So, since the index isn't in the dictionary when CreateShareAndAddBusinessObject() is called, then something within CreateShareAndAddBusinessObject() is adding a record to the dictionary before the call to add the record to the dictionary.  There are 4 operations, you'll need to check into: 1) your BO's constructor 2) some logic happening when the current data table is shared 3) setting the parent relationship, and 4) setting the parent business object.

Something in there is adding to the dictionary before the code to add to the dictionary.  

Try putting a breakpoint on the line for Me._BusinessObjectList.Add(index, loReturn) and see if something hits that before you're expecting it to.

Also, do you have 2 business objects trying to use the same shared data table?  Through the SharedDataTableKey perhaps?
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
By the way, calling .Add() calls .NewRow() internally.  Calling .Add() just also goes through the logic of enabling the bound controls through the editing state.  NewRow() doesn't mess with any of the UI stuff, so that's why it's usually recommended for programmatically adding rows.
jsantos
jsantos
StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)StrataFrame Novice (78 reputation)
Group: StrataFrame Users
Posts: 26, Visits: 87
Hi Ben, 

Thanks for the information.

So I put a break point on Me._BusinessObjectList.Add(index, loReturn) and it gets hit once prior to error for the same child business object. So it goes into CreateShareAndAddBusinessObject 2 times on one call to .NewRow. The first time through no error.

I checked the SharedDataTableKey and from what I can tell the parent and child do not share the same key.
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Yeah, that double call is what's causing the error.  Will you send me a screenshot of the Call Stack window on each of those 2 times that it hits the line to Me._BusinessObjectList.Add()?

That should give us a clue as to why it's hitting it twice.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search