﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » .NewRow() Error - An Item with the same key has already been added.</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 19 May 2026 22:02:27 GMT</lastBuildDate><ttl>20</ttl><item><title>.NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33036.aspx</link><description>Hello,&lt;br/&gt;&lt;br/&gt;&lt;div&gt;I am having an issue when calling .NewRow I get an error in the CreateShareAndAddBusinessObject function in BusinessBindingSource.vb (see attached image)&lt;br/&gt;&lt;br/&gt;&lt;div&gt;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.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Any ideas?&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;img src="http://forum.strataframe.net/Uploads/Images/c855b057-bc96-4ba1-a242-0e50.png" alt="http://forum.strataframe.net/Uploads/Images/c855b057-bc96-4ba1-a242-0e50.png" /&gt;</description><pubDate>Wed, 26 Aug 2015 07:30:04 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33057.aspx</link><description>Hi JSantos, Ben,&lt;br/&gt;&lt;br/&gt;Wow it must have been a long night for you JSantos thinking about this issue &lt;span&gt;:hehe:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;I knew the combination of using filters in such a way had something to do with this issue after all and I am glad you got that figured out.&lt;br/&gt;&lt;br/&gt;Now, it looks like you inherited this project and after this experience, I would be very careful with the miss-used of BO.Filter or child auto filtering in other part of the code.&amp;nbsp; As you have seen, the final exception being thrown by .Net not necessarily point you in the right track as to what is causing the error.&lt;br/&gt;&lt;br/&gt;I learned over the years that StrataFrame design and logic used for the Business Object is rock solid since my applications never fail due to an internal SF error or memory leaks, it is always due to an improper used of a feature in the BO and 99% of the time a BO.Filter or BO.Sort is involve.&lt;br/&gt;&lt;br/&gt;Enjoy!!!</description><pubDate>Wed, 26 Aug 2015 07:30:04 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33056.aspx</link><description>Woot! &amp;nbsp;Glad you're fixed. &amp;nbsp;Let us know when you need help on your next gremlin.</description><pubDate>Wed, 26 Aug 2015 07:15:02 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33055.aspx</link><description>Awesome!!&lt;br/&gt;&lt;br/&gt;&lt;div&gt;So following both your comments it looks like this filtering is causing the issue. Just before the NewRow call I checked the filter and it was set so I set it to empty string at the beginning of the procedure and that seems to have done the trick. NO MORE ERRORS!!! (at least for now) , yippeee.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Thanks to you both Edhy and Ben.&amp;nbsp;</description><pubDate>Wed, 26 Aug 2015 07:12:26 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33054.aspx</link><description>Ok more info.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;I stepped through the code in .NewRow() and actually there is an error occurring in the procedure call OnSetDefaultValues, which raises the&amp;nbsp;SetDefaultValues event.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Thanks Ben I will take a look at the filter at the .NewRow call and take a look at that property.</description><pubDate>Wed, 26 Aug 2015 06:58:24 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33053.aspx</link><description>Yes, there should be at least 1 record in the BO after .NetRow() if there is no filter on it.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;There is a property on the primary business object that will auto-filter child business objects. &amp;nbsp;You might check the value of the .Filter in the watch window before the call to .NewRow() and see if something else, like the parent BO, is setting it.</description><pubDate>Wed, 26 Aug 2015 06:54:08 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33052.aspx</link><description>OK so when I commented all filtering of the msd BO I get no error at the .NewRow call.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;But when I check any of the properties of the msd BO I get "MicroFour.StrataFrame.Business.BusinessLayerException: The CurrentRow for table '[dbo].[sch_MileStonesDependencies]' could not be evaluated because the CurrentRowIndex is out of range. &amp;nbsp;Business object record count: 0. &amp;nbsp;CurrentRowIndex: -1."&lt;br/&gt;&lt;br/&gt;&lt;div&gt;After .NewRow shouldn't there be at least one record in the BO?&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Still investigating.</description><pubDate>Wed, 26 Aug 2015 06:45:50 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33051.aspx</link><description>Thanks both of you your help.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Unfortunately neither suggestions worked. I even tried PausingChangedEvents for the parent as well as the business object that I am trying to add to.&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Edhy, to be honest I have no idea why the filter is there myself that's the way the code was when I picked up this issue. It is looping through the parent BO to get information and creating msd records (child BO).&lt;br/&gt;&lt;br/&gt;&lt;div&gt;OK while typing this message and based on your comments Edhy I decided you know what let me search the entire file where msd.Filter is being used maybe to comment them all out, like you I just don't understand the purpose of filtering the child object at the point while adding to it. There is 0 comments in this code except for this I just found. Look at the Try Catch error handler comment. Lovely.&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;span&gt;[codesnippet]&lt;/span&gt;&amp;nbsp;If ptmd.MoveFirst Then&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;Do&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;msd.Filter = "msd_PrimaryRecID = '" &amp;amp; Me.recid &amp;amp; "'"&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;Try&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;msd.NewRow()&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;Catch ex As ArgumentException&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;'I32065, "An item with the same key has already been added." error&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;'ArgumentException occurs when there is more than one predecessor coming from product template&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;'This error does not seem to be causing functional instability, we can just ignore it for now&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;End Try&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;msd.msd_DependencyType = ptmd.DependencyType&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;msd.msd_LagTime = ptmd.LagTime&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;msd.msd_DependentTaskNumber = CShort(ptmd.DependentTaskNumber + Me._TaskDiff)&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;'-- Dependent RecID will be set in milestone form update&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;Loop While ptmd.MoveNext&lt;br/&gt;&lt;br/&gt;&lt;div&gt;End If[/codesnippet]&lt;br/&gt;&lt;br/&gt;&lt;div&gt;So what I am going to do right now is try commenting out all the filtering used on the msd BO and see what effect it has. I will be right back.</description><pubDate>Wed, 26 Aug 2015 06:35:56 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33050.aspx</link><description>Yes, Ben you are right.&lt;br/&gt;&lt;br/&gt;I remember long time ago when I was starting with SF having this sort of weird outcome when using filter/sorting on a BO and adding new rows or deleting current one.&amp;nbsp; Careful is a most during those situation, but I must also say that I love the BO Filter/Sorting features since they have allowed me to deal with some custom processing in an easy way, again as long as I remember that I using a filter or sort later on &lt;span&gt;:D&lt;/span&gt;</description><pubDate>Tue, 25 Aug 2015 16:26:03 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33049.aspx</link><description>Yep, Edhy, the WPF ICollectionView and IEditableCollectionViewAddNewItem models take care of adding/editing an item when the view is sorted/filtered by holding the item to the side until the add/edit of the item is finalized through CommitAdd/Edit(). &amp;nbsp;There's even a NewItemPlaceholderPosition to allow you to specify whether you want the blank new-item row on the top or the bottom.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Sounds great in theory, but the implementation has been a bugger because you have to have a custom enumerator that return the "fake" or shifted items.</description><pubDate>Tue, 25 Aug 2015 16:04:05 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33048.aspx</link><description>Hi JSantos and Ben,&lt;br/&gt;&lt;br/&gt;One thing to keep in mind is that in your msd BO you have a filter condition but when you add call msd.NewRow() that new record will not show up due to the filter condition, so you should temporarily remove the filter, add the new row, add a value to msd_PrimaryRecID that match the filter then apply the filter.&lt;br/&gt;&lt;br/&gt;Working with filtering in BO can be very tricky and cause a lot of headache if your code don't take then in consideration, specially when looping.&lt;br/&gt;&lt;br/&gt;So I would code your loop like this:&lt;br/&gt;&lt;span&gt;[codesnippet]&lt;br/&gt;&lt;br/&gt;Do&lt;br/&gt;&amp;nbsp;&amp;nbsp; msd.NewRow()&lt;br/&gt;&amp;nbsp;&amp;nbsp; msd._PrimaryRecID = Me.recid&lt;br/&gt;&amp;nbsp;&amp;nbsp; msd.Filter = String.Format("msd_PrimaryRecID = '{0}'", Me.recid)&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&amp;nbsp; .... continue with your code&lt;br/&gt;[/codesnippet]&lt;br/&gt;&lt;br/&gt;Again, I don't see your whole code, but the filter may be the one causing you problems.&lt;br/&gt;&lt;br/&gt;Also to be honest, I don't quite understand why you are applying a filter to the msd BO since you are adding a new record inside the loop.&amp;nbsp; I am assuming that the BO you showing here is not msd.&lt;br/&gt;&lt;/span&gt;</description><pubDate>Tue, 25 Aug 2015 14:59:24 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33047.aspx</link><description>OK, it looks like setting a value on the row is causing the DefaultView to raise a ListChanged event, which is being nested into the middle of the check for whether the record exists in the dictionary and adding the record to the dictionary.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;We can pause the changed events by using wrapping the call to NewRow() with bo.PauseChangedEvents() and bo.ResumeChangedEvents().&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Give that a try and see how that works.</description><pubDate>Tue, 25 Aug 2015 14:30:40 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33046.aspx</link><description>I thought I would do one better and provide the 2 call stacks in one Excel file. I didn't want to exclude anything that may be helpful.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Thanks&lt;div&gt;Jeff</description><pubDate>Tue, 25 Aug 2015 14:23:38 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33045.aspx</link><description>Yeah, that double call is what's causing the error. &amp;nbsp;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()?&lt;br/&gt;&lt;br/&gt;&lt;div&gt;That should give us a clue as to why it's hitting it twice.</description><pubDate>Tue, 25 Aug 2015 14:12:47 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33044.aspx</link><description>Hi Ben,&amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Thanks for the information.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;So I put a break point on&amp;nbsp;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;Me._BusinessObjectList.Add(index, loReturn) and it gets hit once prior to error for the same child business object. So it goes into&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;CreateShareAndAddBusinessObject 2 times on one call to .NewRow. The first time through no error.&lt;/span&gt;&lt;div&gt;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;&lt;br/&gt;&lt;/span&gt;&lt;div&gt;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;I checked the&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;SharedDataTableKey&lt;/span&gt;&lt;span style="font-family: Verdana, Tahoma, Arial; font-size: 13.3333330154419px; line-height: 21.3333339691162px; background-color: rgb(249, 250, 252);"&gt;&amp;nbsp;and from what I can tell the parent and child do not share the same key.&lt;/span&gt;</description><pubDate>Tue, 25 Aug 2015 14:09:33 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33043.aspx</link><description>By the way, calling .Add() calls .NewRow() internally. &amp;nbsp;Calling .Add() just also goes through the logic of enabling the bound controls through the editing state. &amp;nbsp;NewRow() doesn't mess with any of the UI stuff, so that's why it's usually recommended for programmatically adding rows.</description><pubDate>Tue, 25 Aug 2015 13:28:17 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33042.aspx</link><description>Howdy J,&lt;br/&gt;&lt;br/&gt;&lt;div&gt;The CreateShareAndAddBusinessObject() method is called from within the Get of the Item() property on BusinessBindingSource. &amp;nbsp;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. &amp;nbsp;So, if the Me.CreateShareAndAddBusinessObject(index) is being called, then the specified index does not exist within the dictionary. &amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;div&gt;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. &amp;nbsp;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.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Something in there is adding to the dictionary before the code to add to the dictionary. &amp;nbsp;&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Try putting a breakpoint on the line for&amp;nbsp;Me._BusinessObjectList.Add(index, loReturn) and see if something hits that before you're expecting it to.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Also, do you have 2 business objects trying to use the same shared data table? &amp;nbsp;Through the SharedDataTableKey perhaps?</description><pubDate>Tue, 25 Aug 2015 13:27:05 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33041.aspx</link><description>I have also just tried using .Add and I get the same error in the same location&amp;nbsp;CreateShareAndAddBusinessObject.</description><pubDate>Tue, 25 Aug 2015 13:23:04 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33040.aspx</link><description>I have attached a screenshot of the code that I think you will see where it is happening.&lt;br/&gt;&lt;br/&gt;&lt;div&gt;Thanks for your help.</description><pubDate>Tue, 25 Aug 2015 13:15:09 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33039.aspx</link><description>Hi,&lt;br/&gt;&lt;br/&gt;Thanks for the image and yes, NewRow() is recommended for adding rows programmatically. &lt;br/&gt;&lt;br/&gt;Still, can you please explain in a wider scope what you are trying to do?&amp;nbsp; also it will help if you can post the code Sub or Function where you are trying to add the new record?&lt;br/&gt;&lt;br/&gt;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.&amp;nbsp; My solution is to use 2 BO, one for the BBS to show in read only grid and one for make the changes.&amp;nbsp; 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.</description><pubDate>Tue, 25 Aug 2015 12:23:02 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33038.aspx</link><description>Sorry about the image. Added attachment&lt;br/&gt;&lt;br/&gt;&lt;div&gt;We are adding a new row programatically. As I understand .NewRow is&amp;nbsp;recommended for programatically adding new rows.</description><pubDate>Tue, 25 Aug 2015 12:14:12 GMT</pubDate><dc:creator>jsantos</dc:creator></item><item><title>RE: .NewRow() Error - An Item with the same key has already been added.</title><link>http://forum.strataframe.net/FindPost33037.aspx</link><description>Hi JSantos,&lt;br/&gt;&lt;br/&gt;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.&lt;br/&gt;&lt;br/&gt;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.&lt;br/&gt;&lt;br/&gt;If I am wrong, please clarify what you are trying to accomplish.</description><pubDate>Tue, 25 Aug 2015 11:54:37 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item></channel></rss>