tags:

views:

76

answers:

2

In trying to setup a unit test for inserting an item into an SQL Server Express (2008) database using C# Linq I've encountered an error that is causing me some trouble. The Linq code is built using Visual Studio 2008.

The exception is thrown on a system running Windows XP SP2. The Linq works fine and the record is inserted appropriately on a system running Windows 7 Home Premium (64-bit).

I've dropped and re-created the database on the XP system. I've dropped and re-created the DAL and corresponding DBML on the XP system.

Other tables with unit tests for inserts to the same database work just fine.

Inserting a record into the table using a simple insert statement in SQL Server Management Studio works appropriately.

What should I look at to find the source of the problem? What should be done to resolve the problem?

Any insight as to what the error message is really trying to say would be greatly appreciated.

Error Message

System.IndexOutOfRangeException : Index was outside the bounds of the array.

Stack Trace

at 

System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at nUnit.DAL.FacilityTests.AddFacility2() in C:\SVN\SVNRevenue360\Branches\Dev\Code\ProviderAdvantage\nUnit.CoreTests\Tests\DAL\FacilityTests.cs:line 50

Insert Code

        [Test]
    public void AddFacility2() {
        string facilityname = "Test Facility";
        try {
            using (PA.Database.Revenue360DB db = new PA.Database.Revenue360DB(PA.DAL.DataAccess.ConnectionString)) {
                db.Log = Console.Out;
                PA.Database.Facility facility = new PA.Database.Facility();
                facility.id = Guid.NewGuid();
                facility.Name = facilityname;
                facility.Street1 = "";
                facility.Street2 = "";
                facility.Street3 = "";
                facility.City = "";
                facility.State = "";
                facility.Zip = "";
                facility.Description = "";

                db.Facilities.InsertOnSubmit(facility);
                db.SubmitChanges();  // line 50
            }
        } catch (Exception ex) {
            Console.WriteLine(ex.GetType().FullName + ":  " + ex.Message);
            Console.WriteLine(ex.InnerException == null ? 
                "No inner exception" :
                ex.InnerException.GetType().FullName + ":  " + ex.InnerException.Message);
            throw;
        }
    }

I've looked at the following SO questions without insight as to what is causing this particular issue.

http://stackoverflow.com/questions/1087172/why-am-i-getting-index-was-outside-the-bounds-of-the-array http://stackoverflow.com/questions/208533/indexoutofrangeexception-on-queryablesingle http://stackoverflow.com/questions/191690/strange-linq-exception-index-out-of-bounds

+1  A: 

Take a look at this link.

Here are a few excerpts:

A common cause of this error is associations pointing in the wrong direction. (Something that is extremely easy to do if editing the model by hand, partly because the association arrow pointer is in the opposite end of where it would appear in an ER diagram)

and

I was having the same problem and it was due to the fact my primary key was two columns instead of the traditional one. (both guids). When I added a third column that was the sole primary key column, it worked.

UPDATE: Did some more poking around and found this SO post. Looks like it might have something to do with your DBML...

Abe Miessler
@Abe I've looked at both of those links as well. The DBML appears correct. In fact, it works on one computer. The table in question has only a single field primary key.
Mike Chess
When you say "it works on one computer" do you mean that you're running the same code in two places and only getting the error in one?
Abe Miessler
@Abe Yes. The same code is being run on two different computers. One works; the other does not.
Mike Chess
I'd get a diff program and compare the working directory to the not working directory. I've had a lot of luck with csdiff in the past: http://www.componentsoftware.com/Products/csdiff/
Abe Miessler
@Abe - Ultimately one of the relationships was wrong in the DBML. The remaining mystery is why it ever worked in the first place on some systems. The fact that it was working on some machines caused the reluctance to believe that the relationships were wrong.
Mike Chess
Glad to hear you got it all figured out. DBML file has been the source of my problems many times before so i feel you pain.
Abe Miessler
+1  A: 

If the code is working on one machine and not working on another the problem should be somewhere outside. Please check if the .NET Framework version on Windows XP is the same as on Windows 7. Secondly, if its XP SP2 then the Operating System might be the cause because Microsoft has changed a lot in SP3. Also check if the database has same tables. Try to reproduce the problem on the same db, i.e. take backup from Win7 and restore it on the XP (of cause backup your data first). What else? Security permissions and connection string might have an impact too.

SlavaGu