Help - System.Data.DataTable.InsertRow() throws ArgumentOutOfRangeException

J

Jim Meehan

I have a C# program that works one time and fails the next on my deployment
test system. It does not fail on my development system. The deployment
uses the installation procedure created by Visual Studio 2003. The
deployment is installing the .NET Framework ver 1.1, MSDE 2000, MDAC 2.7,
and my application. I am assuming (bad thing to do) that the install is
working, because the application works every so often.

In all cases I am adding a records to a database. In both instances it is
to an empty database and the data being added is the same. When the windows
program fails the error is always an ArgumentOutOfRangeException on the same
row add. The output from my try-catch code is:

ArgumentOutOfRangeException: System.ArgumentOutOfRangeException: Index was
out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
pos)
at System.Data.DataRowCollection.Add(DataRow row)
at WinMCDB.gdsrvmntDataTable.AddgdsrvmntRow(gdsrvmntRow row)
at MCLoader.Form1.AddGdsrvmnt(String monumnt_id, Int32 date_adj, Double
latitude, Double longitude, Double adj_elev, Double geoid_ht, Double
ellips_ht, String localCoordinateSystem)

ParamName: index, Value: null

Does anybody know why the System.Data.DataTable.InsertRow(DataRow row, Int32
proposedID, Int32 pos) method index is null? Or how I can debug this issue
with a System.Data.DataTable method InsertRow.

Jim
 
J

Jim Meehan

Thanks for the reply Miha,

Your questions got me thinking and double checking my code. It looks OK to
me.

The database has three tables A, B, and C. There is a relationship between
A and B, and B and C. The data is being entered correctly for all fields in
each of the three tables. Before I enter a child I check for a parent. If
the parent is not there I add it. If the Parent (A) is there I add it. The
program sequence for adding records to the table has the following sequence.

Look for A with key of [1], No entry
Add A with key of [1]
Look for B with key of [1,a], No entry
Add B with key of [1, a]
Look for A with key of [2], No entry
Add A with key of [2]
Look for B with key of 2,a, No entry
Add B with key of [2, a]
Look for A with key of [3], No entry
Add A with key of [3]
Look for B with key of [3,a], No entry
Add B with key of [3, a]
Look for A with key of [4], No entry
Add A with key of 1
Look for B with key of [4,a], No entry
Add B with key of [4, a]
Add C with key of [4,a,1]
Add C with key of [4,a,2]
Add C with key of [3,a,1]
Add C with key of [3,a,2]
Look for A with key of [1], Found entry
Look for B with key of [1,b],No entry
Add B with key of [1, b], <---------------------{It is at this point the
can fail or succeed. It seems to be random.}
Look for A with key of [2], Found entry
Look for B with key of [2,b],No entry
Add B with key of [2, b], <---------------------{If the previous succeeds
everything work!}
Look for A with key of [3], Found entry
Look for B with key of [3,b],No entry
Add B with key of [3, b]
Look for A with key of [4], Found entry
Look for B with key of [4,b],No entry
Add B with key of [4, b]
Add C with key of [4,b,1]
Add C with key of [4,b,2]
Add C with key of [3,b,1]
Add C with key of [3,b,2]

What I don't understand is how it can be working 100% of the time on my
development and test system, but when I go to another system it randomly
fails. My integration test uses the same input file each time so the data
between runs and the program flows are the same. Nothing changes! The SQL
database is never updated because I have not saved the data yet.

Do you have any idea where I should be looking for the problem?

Jim

Miha Markic said:
Hi Jim,

What is the table definition?
What values are you adding?

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Jim Meehan said:
I have a C# program that works one time and fails the next on my deployment
test system. It does not fail on my development system. The deployment
uses the installation procedure created by Visual Studio 2003. The
deployment is installing the .NET Framework ver 1.1, MSDE 2000, MDAC 2.7,
and my application. I am assuming (bad thing to do) that the install is
working, because the application works every so often.

In all cases I am adding a records to a database. In both instances it is
to an empty database and the data being added is the same. When the windows
program fails the error is always an ArgumentOutOfRangeException on the same
row add. The output from my try-catch code is:

ArgumentOutOfRangeException: System.ArgumentOutOfRangeException: Index was
out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
pos)
at System.Data.DataRowCollection.Add(DataRow row)
at WinMCDB.gdsrvmntDataTable.AddgdsrvmntRow(gdsrvmntRow row)
at MCLoader.Form1.AddGdsrvmnt(String monumnt_id, Int32 date_adj, Double
latitude, Double longitude, Double adj_elev, Double geoid_ht, Double
ellips_ht, String localCoordinateSystem)

ParamName: index, Value: null

Does anybody know why the System.Data.DataTable.InsertRow(DataRow row, Int32
proposedID, Int32 pos) method index is null? Or how I can debug this issue
with a System.Data.DataTable method InsertRow.

Jim
 
M

Miha Markic [MVP C#]

Hi Jim,

Can you show us this piece of code:
Look for B with key of [1,b],No entry
Add B with key of [1, b].
 
J

Jim Meehan

Miha,

Sorry I cannot place the code on a public forum. I have though found the
cause, but I don't know why.

Between the A and the B table I have a relationship. The A table has a
single column used as the primary key, call it the ID. The B table has two
columns used for the primary key, call them ID and Datum. Now the
relationship between A and B use the ID, A's ID is the Key Fields and B's ID
is the Foreign Key Fields. I set the Update rule to "cascade", the Delete
rule to "cascade", and the Accept/Reject rule to "cascade".

Now when I run on my development system and on my deployment test system it
works 100% of the time. When I run in on another system, where I installed
the .NET Framework 1.1, MDAC 2.8, MSDE 2000, and the application it fails
about 70% of the time. It is interesting that the failure is not 100%, it
just randomly fails always on the same row addition. So why does it work
sometime and fail others? If there is a problem with my logic it should fail
all the time or succeed all the time.

This morning I changed all the relationship rules to "none", it now works
100% of the time on all systems. This looks like the anomaly is within the
generated code for the dataset or System.Data.DataTable.InsertRow(DataRow
row, Int32 proposedID, Int32 pos).

Because of the random failures and successes, and the consistent data test
pattern between runs (nothing is changing with in the application's
execution flow); I have to believe the Microsoft Visual Studio generated
code or worse the System.Data.DataTable library is not consistent in its
execution order, i.e. it has problems! Especially when switching from
"cascade" to "none" for the Relationship rules fixes the problem..

Luckily for this application the rules are not needed, but the database
logic is within a common library. Where other applications could/should have
the rules set to "cascade", so ID changes cascade, deletes of the A row
deletes the B rows, etc.

Now I don't know how to track this down without tracing through the logic of
the System.Data.DataTable to see what is happening. Someone from Microsoft
I believe needs to look into this or explain to me the cause. Scenarios
where 1+1 = 2 30% of the time and 1+1 throws an exception 70% of the time on
some computers is not acceptable.

I have my "workaround", but I don't like or trust the ado.net implementation
when switching from cascade to none on relationship rules. Grr...

Regards,

Jim Meehan
CAELUM - Unitec
White Sand Missile Range
Tel: (505) 678-2630


Miha Markic said:
Hi Jim,

Can you show us this piece of code:
Look for B with key of [1,b],No entry
Add B with key of [1, b].

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Jim Meehan said:
Thanks for the reply Miha,

Your questions got me thinking and double checking my code. It looks OK to
me.

The database has three tables A, B, and C. There is a relationship between
A and B, and B and C. The data is being entered correctly for all
fields
in
each of the three tables. Before I enter a child I check for a parent. If
the parent is not there I add it. If the Parent (A) is there I add it. The
program sequence for adding records to the table has the following sequence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top