Insert new Record - Error

Thread is closed for posting
2 posts, 0 answers
  1. Ingo Dignas
    Ingo Dignas avatar
    16 posts
    Member since:
    May 2009

    Posted 18 Jun 2009 Link to this post

    I have an Object called "Contact" (name, lastname ect.) This object includes a country object. On my first web form I collect the users data and put it into the objects:

    Contact myContact = new Contact();
     int cid =int.Parse(rcb_country.SelectedValue.ToString());
    IObjectScope scope1 = ObjectScopeProvider1.GetNewObjectScope();
              var cselected = from a in scope1.Extent<Country>()
                                where a.CountryID == cid
                                select a;
    scope1.Transaction.Begin();
    foreach (Country coun in cselected)
                    myContact.Country = coun;
    scope1.Transaction.Commit();


    Ok....this works fine.
    On my last page I try to insert the contact into my database, like this:

    if (cb_agb.Checked)
    {
                    IObjectScope scope2 = ObjectScopeProvider1.GetNewObjectScope();
                    try
                    {
                       
                        scope2.Transaction.Begin();
                        scope2.Add(myContact);
                        scope2.Transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        scope2.Transaction.Rollback();
     
                    }
    }

    Now I get an error: object references between two different objects scopes are not allowed.........the object ...country is already managed

    What can it be?



  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 23 Jun 2009 Link to this post

    Hi Joerg Hermann,
    you are getting this exception because you are indeed trying to work with two different scopes. To avoid this you should not create a new instance of the scope but use the current one instead. This can be done by modifying your code a little:
    IObjectScope scope2 = ObjectScopeProvider1.ObjectScope(); 
    This way you will get a reference to your current scope instead of creating a second one.
    All the best,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top