none
"Index was outside the bounds of the array" on submitchanges() RRS feed

  • Question

  • Hello,

    I am using Visual Studio 2008 and I am using linq to SQL .I have tried linq on some tables by inserting,updating and deleting and all my work was doing fine.But I am now facing a problem.

    In my database,I have 3 tables called:"Xinventory","XInfrigidation" and XinfrigidationDetails"

    Xinventory>XinfrigidationDetails is a relationship one to many in which IdInventory is the key

    XInfrigidation>XinfrigidationDetails  is a relationship one to many in which IdInfrigidation is the key

    When I try to add a new object (ObjXInfrigidationDetail) to the XInfrigidationDetails,I must do 2 Add unless it will give me an error "Index was outside the bounds of the array"

    So I will do:

    ObjXInventory.XInfrigidationDetails.Add(ObjXinfrigidationDetail)

    ObjXinfrigidation.XInfrigidationDetails.Add(ObjXinfrigidationDetail)

    So with the add,it worked fine...

    But when I retreive an object from the XinfrigidationDetail using its primary key "IdInfrigidationDetail" to update it.On the debug,after updating it,on the submitchanges() ,it will give me an error "Index was outside the bounds of the array"

    Th XInfrigidationDetail has only one primary key which is "IdInfrigidationDetail".To resolve this error,I tried to remove this key and make IdInternalInventory and IdInfrigidation the primary key for the table .But the error is still remaining.

    In my situation,an object in the XInfrigidationDetail has 2 parents.

    So I don't know if linq can support it.

    Please, I want to understand the cause for my issue and I need a solution to solve it.

    Thank you in advance

     

     

     

     


    Sam85
    Friday, January 30, 2009 8:38 AM

Answers

  • You will get this error if the parent side of an association does not refer to the primary key. This is a known issue that is being resolved as part of .NET 4.0.

    [)amien
    Tuesday, February 3, 2009 8:31 AM
    Moderator

All replies

  • I got a similar thing once. I had a many-to-many junction table from a table back to itself. Removing the associations between the tables involved and re-adding them fixed it. If my memory serves me right I had one or both of the associations backwards.
    Friday, January 30, 2009 8:57 AM
    Answerer
  • Thank you a lot !Your solution worked!!!!

    Thank you again!

    But the question is there any other solutions for that?like how we can retreive an object from the table that has 2 parents?Because I think this is the problem

     


    Sam85
    Friday, January 30, 2009 1:53 PM
  • sam.m said:

    But the question is there any other solutions for that?like how we can retreive an object from the table that has 2 parents?Because I think this is the problem


     

    By "2 parents", do you mean a composite primary key that consists of two foreign keys? That is not a problem - L2S handles that just fine. If you get "out of bounds" errors when dealing with a tables like that then you need to check the direction of the associations. It is very easy to get the associations backwards when using the designer if you're used to working in data modelling tools. Note that the class designer has the association arrows pointing 'backwards' compared to the FK arrows in many data modelling tools, e.g.:

    Data model diagram:

    ER diagram / data model

    Class designer in VS:

    VS2008 class designer


    Kristofer Huagati Systems Co., Ltd. web: http://www.huagati.com/ blog: http://blog.huagati.com/ twitter: http://twitter.com/KristoferA
    Friday, January 30, 2009 4:24 PM
    Answerer
  • You will get this error if the parent side of an association does not refer to the primary key. This is a known issue that is being resolved as part of .NET 4.0.

    [)amien
    Tuesday, February 3, 2009 8:31 AM
    Moderator
  • If you post the call stack for the exception, we can verify for sure if you are hitting the bug Damien mentioned, but from your description of the problem, it sounds likely.

    Thanks,
    Sarah
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 5, 2009 11:44 PM
    Moderator