There is no row at position x

M

MR

I am getting the exception "There is no row at position 5" on a dataview
that has 10 rows (count = 10)
The original dataset, i.e. unfiltered, has 12 rows. when it is filtered,
there are 10. i can look in the dataview and see all ten rows. but when i am
in the foreach(DataRowViw pr in orderDV) loop i get the error when i try to
access a field in the sixth row (position 5).
i cannot figure out why i keep getting this exception . does anyone know
why?
thanks
m
I have included the code that causes this error:

string Filter = "BatchID = " + BatchID.ToString(); // usullay BatchID = 0

DS.Tables[OrderTable].DefaultView.RowFilter = Filter;

DataView orderDV = DS.Tables[OrderTable].DefaultView;

foreach(DataRowView pr in orderDV)

{

string Orders_Id = pr["OrderNumber"].ToString(); // why do i get an
exception here?

//pr["OrderNumber"]
{"There is no row at position 5." } System.IndexOutOfRangeException



foreach (DataRow cr in pr.Row.GetChildRows("OrderCollection"))

{

}

pr["BatchID"] = 1; // assign new BatchID

}
 
K

Kevin Yu [MSFT]

Hi MR,

Could you try to check in the DataTable.Rows collection that if this row
has been deleted? If a row is deleted, the cell values have to be accessed
with a DataRowVersion argument to get the original value.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

MR

upon closer examination, i see that the DataViews count = 5.
so how does the foreach iterator/index allow more than 5 iterations?
 
B

Bart Mermuys

Hi,

MR said:
I am getting the exception "There is no row at position 5" on a dataview
that has 10 rows (count = 10)
The original dataset, i.e. unfiltered, has 12 rows. when it is filtered,
there are 10. i can look in the dataview and see all ten rows. but when i
am in the foreach(DataRowViw pr in orderDV) loop i get the error when i
try to access a field in the sixth row (position 5).
i cannot figure out why i keep getting this exception . does anyone know
why?
thanks
m
I have included the code that causes this error:

string Filter = "BatchID = " + BatchID.ToString(); // usullay BatchID = 0

DS.Tables[OrderTable].DefaultView.RowFilter = Filter;

DataView orderDV = DS.Tables[OrderTable].DefaultView;

foreach(DataRowView pr in orderDV)

{

string Orders_Id = pr["OrderNumber"].ToString(); // why do i get an
exception here?


//pr["OrderNumber"] {"There is no row at position 5." }
System.IndexOutOfRangeException



foreach (DataRow cr in pr.Row.GetChildRows("OrderCollection"))

{

}

pr["BatchID"] = 1; // assign new BatchID

Yes, but if there is a filter like "BatchID = 0" then this DataRowView will
dissappear from the DataView because it no longer passes the filter now.

If you are certain that all rows will get excluded because of the filter,
then you could use a loop like :

string Filter = "BatchID = " + BatchID.ToString(); // usullay BatchID = 0
DS.Tables[OrderTable].DefaultView.RowFilter = Filter;
DataView orderDV = DS.Tables[OrderTable].DefaultView;

while ( orderDV.Count > 0 )
{
DataRowView pr = orderDV[0];
string Orders_Id = pr["OrderNumber"].ToString();
....

// this rows gets excluded because of rowfilter and so count decreases
pr["BatchID"] = 1;
pr.EndEdit();
}

HTH,
Greetings
 
M

MR

yes of course!
i forgot that the filter is dynamic
thanks
m

Bart Mermuys said:
Hi,

MR said:
I am getting the exception "There is no row at position 5" on a dataview
that has 10 rows (count = 10)
The original dataset, i.e. unfiltered, has 12 rows. when it is filtered,
there are 10. i can look in the dataview and see all ten rows. but when i
am in the foreach(DataRowViw pr in orderDV) loop i get the error when i
try to access a field in the sixth row (position 5).
i cannot figure out why i keep getting this exception . does anyone know
why?
thanks
m
I have included the code that causes this error:

string Filter = "BatchID = " + BatchID.ToString(); // usullay BatchID = 0

DS.Tables[OrderTable].DefaultView.RowFilter = Filter;

DataView orderDV = DS.Tables[OrderTable].DefaultView;

foreach(DataRowView pr in orderDV)

{

string Orders_Id = pr["OrderNumber"].ToString(); // why do i get an
exception here?


//pr["OrderNumber"] {"There is no row at position 5." }
System.IndexOutOfRangeException



foreach (DataRow cr in pr.Row.GetChildRows("OrderCollection"))

{

}

pr["BatchID"] = 1; // assign new BatchID

Yes, but if there is a filter like "BatchID = 0" then this DataRowView
will dissappear from the DataView because it no longer passes the filter
now.

If you are certain that all rows will get excluded because of the filter,
then you could use a loop like :

string Filter = "BatchID = " + BatchID.ToString(); // usullay BatchID = 0
DS.Tables[OrderTable].DefaultView.RowFilter = Filter;
DataView orderDV = DS.Tables[OrderTable].DefaultView;

while ( orderDV.Count > 0 )
{
DataRowView pr = orderDV[0];
string Orders_Id = pr["OrderNumber"].ToString();
....

// this rows gets excluded because of rowfilter and so count decreases
pr["BatchID"] = 1;
pr.EndEdit();
}

HTH,
Greetings
 

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