Removing from a collection

Posts   
 
    
das330
User
Posts: 4
Joined: 08-Mar-2005
# Posted on: 11-Mar-2005 18:12:05   

I'm attempting to remove a row from a collection without any luck.

my collection is called companies and it contains 7 items. Using the Remove method I get an error that the Object reference not set to an instance of an object.

Code looks like:

CompanyEntity tmp = new CompanyEntity(pkGuid); <--- can examine tmp looks good! companies.Remove(tmp);

So I tried to use the RemoveAt and I get the following error: Specified cast is not valid

CompanyEntity tmp = new CompanyEntity(pkGuid);

if (companies.Count > 0 ) <-- count =7 { int index = companies.IndexOf(tmp); <-- Value =3 companies.RemoveAt( companies.IndexOf(tmp) ); <-- Cast error }

What am I doing wrong?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Mar-2005 18:51:41   

Could you please paste the stacktrace as well? I pressume you're using selfservicing?

Frans Bouma | Lead developer LLBLGen Pro
das330
User
Posts: 4
Joined: 08-Mar-2005
# Posted on: 14-Mar-2005 14:53:39   

I'm using self-service.

Stack trace from remove

[NullReferenceException: Object reference not set to an instance of an object.] SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase.Remove(IEntity entityToRemove) +174 WebMaint.CompanyGrid.UltraWebToolbar1_ButtonClicked(Object sender, ButtonEvent be) in c:\projects\terry\nlm3\webmaint\companygrid.aspx.cs:116 Infragistics.WebUI.UltraWebToolbar.UltraWebToolbar.OnButtonClicked(TBarButton button, Boolean bDepressed) +69 Infragistics.WebUI.UltraWebToolbar.UltraWebToolbar.RaisePostBackEvent(String eventArgument) +415 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138 System.Web.UI.Page.ProcessRequestMain() +1292

Stack Trace from removeat

InvalidCastException: Specified cast is not valid.] SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase.RemoveAt(Int32 index) +34 WebMaint.CompanyGrid.UltraWebToolbar1_ButtonClicked(Object sender, ButtonEvent be) in c:\projects\terry\nlm3\webmaint\companygrid.aspx.cs:120 Infragistics.WebUI.UltraWebToolbar.UltraWebToolbar.OnButtonClicked(TBarButton button, Boolean bDepressed) +69 Infragistics.WebUI.UltraWebToolbar.UltraWebToolbar.RaisePostBackEvent(String eventArgument) +415 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138 System.Web.UI.Page.ProcessRequestMain() +1292

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Mar-2005 18:46:56   

I can't figure out why it doesn't work. Here's the remove routine:


/// <summary>
/// Remove given IEntity from the list.
/// </summary>
/// <param name="entityToRemove">Entity object to remove from list.</param>
public void Remove(IEntity entityToRemove)
{
    int index = List.IndexOf(entityToRemove);
    if(index >=0)
    {
        if(BeforeRemove!=null)
        {
            BeforeRemove(entityToRemove, new EventArgs());
        }
        List.Remove(entityToRemove);
        _contentInOriginalOrder.Remove(entityToRemove);

        if(_containingEntity!=null)
        {
            entityToRemove.UnsetRelatedEntity(_containingEntity, _containingEntityMappedField);
        }

        OnListChanged(index, ListChangedType.ItemDeleted);

        // remove subscribtion to the changed event.
        entityToRemove.EntityContentsChanged -= new EventHandler(EntityInListOnEntityContentsChanged);
    }
} 

nowhere in there, it can run into a null reference unexpected, only when you pass in null yourself (which you don't, as you said).

If I start a simple test:


[Test]
public void RemoveFromCollectionTest()
{
    ProductCollection products = new ProductCollection();
    products.Add(EntityCreator.CreateNewProduct(1));
    products.Add(EntityCreator.CreateNewProduct(2));
    products[0].TestRunId = _testRunID;
    products[1].TestRunId = _testRunID;

    Assert.IsTrue((products.SaveMulti() > 0));

    ProductEntity toRemove = new ProductEntity(products[0].ProductId);

    products.Remove(toRemove);
}

It works (this entity uses a GUID as pk, so I thought it would be similar to your situation).

I'll now mail you a debug build of the ormsupport classes. Could you please place both files in the .zip in the bin folder of your application and test again? This will then give a stack trace which should have line numbers simple_smile

Frans Bouma | Lead developer LLBLGen Pro
das330
User
Posts: 4
Joined: 08-Mar-2005
# Posted on: 14-Mar-2005 21:29:36   

Debug libraries pointed me in the right direction. Thank you.

I was referencing the 1.0 dll's instead of the 1.1

Changing that and both removes now work.

Sorry for the confusion.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Mar-2005 22:46:39   

great! simple_smile Glad it's solved! simple_smile

Frans Bouma | Lead developer LLBLGen Pro